Add copy title function

This commit is contained in:
Tiger Ren 2024-09-25 15:04:39 +08:00
parent 9118703f7b
commit f6c3d4d121
3 changed files with 44 additions and 14 deletions

View File

@ -66,6 +66,16 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
performRmdownActions();
sendResponse({ success: 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
}
});

View File

@ -3,9 +3,14 @@
<head>
<title>Auto Download Link</title>
<script src="popup.js"></script>
<style>
body { width: 200px; padding: 10px; }
button { margin: 5px 0; width: 100%; }
</style>
</head>
<body>
<h1>Auto Download Link</h1>
<button id="start">Start</button>
<button id="copyTitle">Copy Title</button>
</body>
</html>

View File

@ -1,17 +1,12 @@
document.addEventListener('DOMContentLoaded', () => {
const startButton = document.getElementById('start');
const copyTitleButton = document.getElementById('copyTitle');
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);
@ -21,8 +16,28 @@ document.addEventListener('DOMContentLoaded', () => {
});
});
});
});
} else {
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");
}
});