让 downie chrome插件支持自定义下载
John Doe让 downie chrome插件支持自定义下载
我有一个自定义的页面,里面包含了许多代下载的内容。
每次需要下载的时候, 都得复制所有链接然后粘贴到downie 里面下载。既然downie插件的源码是开源的。 就修改一下。

为了获取页面里面的链接, 需要新建一个content_script. 不然无法获取document。

然后新增一个右键菜单, 注册自定义函数来处理相关逻辑


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| function downloadLocal(tab) { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { const currentTab = tabs[0]; chrome.tabs.sendMessage(currentTab.id, { action: "getTransUrls" }, (response) => { if (response && response.urls) { console.log(response.urls); response.urls.forEach((url) => { openLinkInDownie(url, 'mp4', tab) }) // You can process the document here as needed } else { console.error("No response from content script."); } }); }); }
|
downloadLocal 发送了一个事件。 在content.js 里面接收, content.js 获取需要下载的链接。 然后将数据返回给downloadLocal里面的回调函数。 调用openLinkInDownie 发送给downie 下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === "getTransUrls") { const a = document.querySelectorAll('.timeline-body'); const b = Array.from(a); const urlsArg = []; b.forEach(item => { const urlRegex = /https?:\/\/[^\s]+/g; const innerText = item.innerText; const urls = innerText.match(urlRegex); if (urls && urls.length) { const url = urls[0]; urlsArg.push(url); } }); console.log('发送连接', urlsArg) sendResponse({ urls: urlsArg }); } });
|