-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
61 lines (50 loc) · 2.03 KB
/
popup.js
File metadata and controls
61 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 1. Logic for the Extract Button
document.getElementById("grabBtn").addEventListener("click", () => {
browser.tabs.query({active: true, currentWindow: true}).then((tabs) => {
const currentTabId = tabs[0].id;
browser.scripting.executeScript({
target: { tabId: currentTabId },
func: scrapeUrls
}).then((results) => {
const extractedUrls = results[0].result;
const textArea = document.getElementById("urlOutput");
textArea.value = extractedUrls;
// Enable the copy button now that we have text
const copyBtn = document.getElementById("copyBtn");
copyBtn.disabled = false;
copyBtn.innerText = "Copy to Clipboard"; // Reset text if it said "Copied!"
}).catch(err => {
console.error("Failed:", err);
document.getElementById("urlOutput").value = "Error: " + err.message;
});
});
});
// 2. Logic for the Copy Button
document.getElementById("copyBtn").addEventListener("click", () => {
const textArea = document.getElementById("urlOutput");
const copyBtn = document.getElementById("copyBtn");
// Copy text to clipboard
navigator.clipboard.writeText(textArea.value).then(() => {
// Visual feedback: Change button text
copyBtn.innerText = "Copied!";
// Reset button text after 2 seconds
setTimeout(() => {
copyBtn.innerText = "Copy to Clipboard";
}, 2000);
});
});
// The scraper function (runs inside the page)
function scrapeUrls() {
var urls = "";
var elements = document.querySelectorAll("ytd-playlist-panel-video-renderer");
if (elements.length === 0) {
return "No playlist videos found. Make sure you are on a YouTube Playlist page.";
}
elements.forEach(function(element) {
var link = element.querySelector("#wc-endpoint");
if (link) {
urls += link.href.split("&")[0] + "\n";
}
});
return urls;
}