增加延时,防止被限流

This commit is contained in:
Tiger Ren 2024-09-25 08:53:31 +08:00
parent 2ee3b49adc
commit 9118703f7b
3 changed files with 54 additions and 14 deletions

View File

@ -1,3 +1,3 @@
# chrome-extension-t66ylink-extractor # chrome-extension-t66ylink-extractor
["*://*.t66y.com/*","*://*.rmdown.com/*"] ["*://*.t66y.com/*","*://*.rmdown.com/*"]

View File

@ -1,18 +1,36 @@
let retryTimer = null;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "openLink") { if (request.action === "openLink") {
chrome.tabs.create({ url: request.url }, (tab) => { chrome.tabs.create({ url: request.url }, (tab) => {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) { chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) {
if (tabId === tab.id && changeInfo.status === 'complete') { if (tabId === tab.id && changeInfo.status === 'complete') {
chrome.tabs.sendMessage(tabId, { action: "performActions" }, (response) => { performActionsWithRetry(tabId);
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
} else {
console.log("Actions performed successfully");
}
});
chrome.tabs.onUpdated.removeListener(listener); chrome.tabs.onUpdated.removeListener(listener);
} }
}); });
}); });
} else if (request.action === "rateLimitReached") {
// Set a timer to retry after 1 hour
if (retryTimer) {
clearTimeout(retryTimer);
}
retryTimer = setTimeout(() => {
chrome.tabs.query({url: "*://rmdown.com/*"}, (tabs) => {
if (tabs.length > 0) {
performActionsWithRetry(tabs[0].id);
}
});
}, 60 * 60 * 1000); // 1 hour in milliseconds
} }
}); });
function performActionsWithRetry(tabId) {
chrome.tabs.sendMessage(tabId, { action: "performActions" }, (response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
} else {
console.log("Actions performed successfully");
}
});
}

View File

@ -11,17 +11,39 @@ function simulateClick(element) {
// Function to perform actions on rmdown.com // Function to perform actions on rmdown.com
function performRmdownActions() { function performRmdownActions() {
console.log("Performing actions on rmdown.com"); console.log("Performing actions on rmdown.com");
// Check if the page contains the rate limit message
if (document.body.innerText.includes("Your IP downloads reached the site limit")) {
console.log("Rate limit reached. Stopping actions.");
// Optionally, you can display a notification to the user here
return;
}
// Random delay between 2 to 5 seconds before starting actions
const initialDelay = 2000 + Math.random() * 3000;
setTimeout(() => { setTimeout(() => {
const copyButton = document.getElementById('cbtn'); const copyButton = document.getElementById('cbtn');
const downloadButton = document.querySelector('button[title="Download file"]'); if (copyButton) {
if (copyButton && downloadButton) { console.log("Copy button found, clicking...");
console.log("Buttons found:", copyButton, downloadButton);
simulateClick(copyButton); simulateClick(copyButton);
simulateClick(downloadButton);
// Random delay between 1 to 3 seconds before next action
const downloadDelay = 1000 + Math.random() * 2000;
setTimeout(() => {
const downloadButton = document.querySelector('button[title="Download file"]');
if (downloadButton) {
console.log("Download button found, clicking...");
simulateClick(downloadButton);
} else {
console.log("Download button not found");
}
}, downloadDelay);
} else { } else {
console.log("Buttons not found"); console.log("Copy button not found");
} }
}, 2000); // Adjust the timeout as needed }, initialDelay);
} }
// Listen for messages from the popup script // Listen for messages from the popup script