Add copy title function
This commit is contained in:
parent
9118703f7b
commit
f6c3d4d121
10
content.js
10
content.js
|
|
@ -66,6 +66,16 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
performRmdownActions();
|
performRmdownActions();
|
||||||
sendResponse({ success: true });
|
sendResponse({ success: true });
|
||||||
return true;
|
return true;
|
||||||
|
} else if (request.action === "copyTitle") {
|
||||||
|
const pageTitle = document.title;
|
||||||
|
navigator.clipboard.writeText(pageTitle).then(() => {
|
||||||
|
console.log("Title copied to clipboard:", pageTitle);
|
||||||
|
sendResponse({ success: true, title: pageTitle });
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("Failed to copy title:", err);
|
||||||
|
sendResponse({ success: false, error: err.message });
|
||||||
|
});
|
||||||
|
return true; // Indicates that the response is sent asynchronously
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,14 @@
|
||||||
<head>
|
<head>
|
||||||
<title>Auto Download Link</title>
|
<title>Auto Download Link</title>
|
||||||
<script src="popup.js"></script>
|
<script src="popup.js"></script>
|
||||||
|
<style>
|
||||||
|
body { width: 200px; padding: 10px; }
|
||||||
|
button { margin: 5px 0; width: 100%; }
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Auto Download Link</h1>
|
<h1>Auto Download Link</h1>
|
||||||
<button id="start">Start</button>
|
<button id="start">Start</button>
|
||||||
|
<button id="copyTitle">Copy Title</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
43
popup.js
43
popup.js
|
|
@ -1,28 +1,43 @@
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const startButton = document.getElementById('start');
|
const startButton = document.getElementById('start');
|
||||||
|
const copyTitleButton = document.getElementById('copyTitle');
|
||||||
|
|
||||||
if (startButton) {
|
if (startButton) {
|
||||||
startButton.addEventListener('click', () => {
|
startButton.addEventListener('click', () => {
|
||||||
console.log("Start button clicked");
|
console.log("Start button clicked");
|
||||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||||
console.log("Active tab:", tabs[0]);
|
console.log("Active tab:", tabs[0]);
|
||||||
|
chrome.tabs.sendMessage(tabs[0].id, { action: "initiateDownload" }, (response) => {
|
||||||
// Inject the content script
|
if (chrome.runtime.lastError) {
|
||||||
chrome.scripting.executeScript({
|
console.error("Error:", chrome.runtime.lastError.message);
|
||||||
target: { tabId: tabs[0].id },
|
} else {
|
||||||
files: ['content.js']
|
console.log("Message sent to content script, response:", response);
|
||||||
}, () => {
|
}
|
||||||
// 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 {
|
} else {
|
||||||
console.error("Start button not found");
|
console.error("Start button not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (copyTitleButton) {
|
||||||
|
copyTitleButton.addEventListener('click', () => {
|
||||||
|
console.log("Copy Title button clicked");
|
||||||
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||||
|
chrome.tabs.sendMessage(tabs[0].id, { action: "copyTitle" }, (response) => {
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
console.error("Error:", chrome.runtime.lastError.message);
|
||||||
|
} else {
|
||||||
|
console.log("Title copied:", response.title);
|
||||||
|
copyTitleButton.textContent = "Title Copied!";
|
||||||
|
setTimeout(() => {
|
||||||
|
copyTitleButton.textContent = "Copy Title";
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.error("Copy Title button not found");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Loading…
Reference in New Issue