-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_search_img.js
161 lines (135 loc) · 7.14 KB
/
main_search_img.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let currentUrl = window.location.href;
let PostList = [];
let currentIndex = 0;
let link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css';
document.head.appendChild(link);
let finalpage = 0;
let isMoreResult = true;
async function logMovies(tags) {
const limit = 966;
let page = 0;
let hasMoreResults = true;
const maxPosts = 5000;
while (hasMoreResults) {
const response = await fetch(`https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&tags=${tags}&pid=${page}&limit=${limit}`);
const text = await response.text();
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(text, "text/xml");
let posts = xmlDoc.querySelectorAll('post');
if (posts.length === 0) {
hasMoreResults = false;
} else {
posts.forEach(post => {
let fileUrl = post.getAttribute('file_url');
let id = post.getAttribute('id');
PostList.push({ fileUrl, id });
});
page += 1;
if (PostList.length >= maxPosts) {
console.log(`Reached maximum allowed posts: ${maxPosts}. Stopping further fetches.`);
const nextResponse = await fetch(`https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&tags=${tags}&pid=${page}&limit=${limit}`);
const nextText = await nextResponse.text();
let nextXmlDoc = parser.parseFromString(nextText, "text/xml");
let nextPosts = nextXmlDoc.querySelectorAll('post');
if (nextPosts.length === 0 || nextPosts.length < limit) {
isMoreResult = false;
} else {
isMoreResult = true;
}
console.log(`Next batch check: ${nextPosts.length} posts found. isMoreResult = ${isMoreResult}`);
hasMoreResults = false;
}
}
console.log(`Processed page ${page}, collected ${PostList.length} media files.`);
}
finalpage = page
console.log(`Total collected ${PostList.length} media files.`);
console.log(`Last page ${finalpage}`);
}
async function processTags() {
let inputTags = document.querySelector('input[name="tags"]');
if (inputTags) {
let tags = inputTags.value.replace(/ /g, "+");
console.log("Input tags: ", tags);
await logMovies(tags);
// Динамічний імпорт через import()
import(chrome.runtime.getURL('utilities/gallery_ui/gallery_mod.js')).then((module) => {
if(isMoreResult){
module.createModal({ isFavorite: false, lastPage: finalpage, postList: PostList});
}else{
module.createModal({isFavorite: false, postList: PostList});
}
module.showModal(0); // Відкриваємо модальне вікно з першим елементом
}).catch(err => {
console.error("Error loading gallery_mod.js: ", err);
});
} else {
console.log("Input element with name='tags' not found.");
}
}
// Створюємо кнопку у боковій панелі, якщо ми на відповідній сторінці
if (currentUrl.includes("page=post") && currentUrl.includes("s=list")) {
let sidebar = document.querySelector('.sidebar');
if (sidebar) {
let tagSearchContainer = sidebar.querySelector('.tag-search');
if (tagSearchContainer) {
let existingPanels = tagSearchContainer.parentElement.querySelectorAll('.panel');
if (existingPanels.length === 0) {
// Використовуємо створення фіксованої панелі з 1 вертикальною кнопкою і 4 горизонтальними
const panel = document.createElement('div');
panel.className = 'panel';
const buttonContainer = document.createElement('div');
buttonContainer.className = 'button-container';
// Створення вертикальних кнопок
const verticalButtons = document.createElement('div');
verticalButtons.className = 'vertical-buttons';
const addNewTagsToSavedButton = document.createElement('button');
addNewTagsToSavedButton.className = 'button vertical-button';
addNewTagsToSavedButton.id = 'addNewTagsToSavedButton';
addNewTagsToSavedButton.textContent = 'Save search tags';
verticalButtons.appendChild(addNewTagsToSavedButton);
const galleryModButton = document.createElement('button');
galleryModButton.className = 'button vertical-button';
galleryModButton.textContent = 'Gallery mod';
galleryModButton.onclick = processTags; // Ваша функція
verticalButtons.appendChild(galleryModButton);
// Додаємо вертикальні кнопки до контейнера
buttonContainer.appendChild(verticalButtons);
// Додаємо роздільник
const divider = document.createElement('div');
divider.className = 'divider';
buttonContainer.appendChild(divider);
// Створення горизонтальних кнопок
const horizontalButtons = document.createElement('div');
horizontalButtons.className = 'horizontal-buttons';
const button = document.createElement('button');
button.id = "open-search-history"
button.className = 'button horizontal-button';
button.innerHTML = '<i class="fa fa-history"></i>'
horizontalButtons.appendChild(button);
const button2 = document.createElement('button');
button2.id = "open-saved-search"
button2.className = 'button horizontal-button';
button2.innerHTML = '<i class="fa fa-copy"></i>';
horizontalButtons.appendChild(button2);
const button3 = document.createElement('button');
button3.id = "open-user-albums"
button3.className = 'button horizontal-button';
button3.innerHTML = '<i class="fa fa-photo-film"></i>';
horizontalButtons.appendChild(button3);
// Додаємо горизонтальні кнопки до контейнера
buttonContainer.appendChild(horizontalButtons);
// Додаємо контейнер до панелі
panel.appendChild(buttonContainer);
// Вставляємо панель після tagSearchContainer
tagSearchContainer.insertAdjacentElement('afterend', panel);
}
} else {
console.log("Element with class='tag-search' not found in sidebar.");
}
} else {
console.log("Element with class='sidebar' not found on the page.");
}
}