The first working version

This commit is contained in:
Tiger Ren 2024-09-25 00:59:06 +08:00
parent ab6f97e1e8
commit 2ee3b49adc
6 changed files with 138 additions and 0 deletions

View File

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

18
background.js Normal file
View File

@ -0,0 +1,18 @@
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') {
chrome.tabs.sendMessage(tabId, { action: "performActions" }, (response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
} else {
console.log("Actions performed successfully");
}
});
chrome.tabs.onUpdated.removeListener(listener);
}
});
});
}
});

53
content.js Normal file
View File

@ -0,0 +1,53 @@
// Function to simulate a click event
function simulateClick(element) {
const event = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
element.dispatchEvent(event);
}
// Function to perform actions on rmdown.com
function performRmdownActions() {
console.log("Performing actions on rmdown.com");
setTimeout(() => {
const copyButton = document.getElementById('cbtn');
const downloadButton = document.querySelector('button[title="Download file"]');
if (copyButton && downloadButton) {
console.log("Buttons found:", copyButton, downloadButton);
simulateClick(copyButton);
simulateClick(downloadButton);
} else {
console.log("Buttons not found");
}
}, 2000); // Adjust the timeout as needed
}
// Listen for messages from the popup script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Message received in content script:", request);
if (request.action === "initiateDownload") {
// Find the link on the page
const link = document.querySelector('a[href*="rmdown.com/link.php?hash="]');
if (link) {
console.log("Link found:", link);
// Open the link in a new tab
chrome.runtime.sendMessage({ action: "openLink", url: link.href });
sendResponse({ success: true });
} else {
console.log("Link not found");
sendResponse({ success: false });
}
return true; // Indicates that the response is sent asynchronously
} else if (request.action === "performActions") {
performRmdownActions();
sendResponse({ success: true });
return true;
}
});
// Automatically perform actions if on rmdown.com
if (window.location.hostname === "rmdown.com") {
performRmdownActions();
}

27
manifest.json Normal file
View File

@ -0,0 +1,27 @@
{
"manifest_version": 3,
"name": "Auto Download Link",
"version": "1.0",
"description": "Automate the process of copying and downloading a link.",
"permissions": [
"activeTab",
"scripting",
"tabs"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["*://*.t66y.com/*", "*://rmdown.com/*"],
"js": ["content.js"]
}
],
"host_permissions": [
"*://*.t66y.com/*",
"*://rmdown.com/*"
]
}

11
popup.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Auto Download Link</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Auto Download Link</h1>
<button id="start">Start</button>
</body>
</html>

28
popup.js Normal file
View File

@ -0,0 +1,28 @@
document.addEventListener('DOMContentLoaded', () => {
const startButton = document.getElementById('start');
if (startButton) {
startButton.addEventListener('click', () => {
console.log("Start button clicked");
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
console.log("Active tab:", tabs[0]);
// Inject the content script
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ['content.js']
}, () => {
// After injection, send the message
chrome.tabs.sendMessage(tabs[0].id, { action: "initiateDownload" }, (response) => {
if (chrome.runtime.lastError) {
console.error("Error:", chrome.runtime.lastError.message);
} else {
console.log("Message sent to content script, response:", response);
}
});
});
});
});
} else {
console.error("Start button not found");
}
});