增加延时,防止被限流

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,8 +1,31 @@
let retryTimer = null;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "openLink") {
chrome.tabs.create({ url: request.url }, (tab) => {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) {
if (tabId === tab.id && changeInfo.status === 'complete') {
performActionsWithRetry(tabId);
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);
@ -10,9 +33,4 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Actions performed successfully");
}
});
chrome.tabs.onUpdated.removeListener(listener);
}
});
});
}
});

View File

@ -11,17 +11,39 @@ function simulateClick(element) {
// Function to perform actions on rmdown.com
function performRmdownActions() {
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(() => {
const copyButton = document.getElementById('cbtn');
const downloadButton = document.querySelector('button[title="Download file"]');
if (copyButton && downloadButton) {
console.log("Buttons found:", copyButton, downloadButton);
if (copyButton) {
console.log("Copy button found, clicking...");
simulateClick(copyButton);
// 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("Buttons not found");
console.log("Download button not found");
}
}, 2000); // Adjust the timeout as needed
}, downloadDelay);
} else {
console.log("Copy button not found");
}
}, initialDelay);
}
// Listen for messages from the popup script