尝试制作一个抓取工具来从网站下载文件

问题描述 投票:0回答:0

我一直在尝试为 Chrome 构建一个插件,它将把页面上的所有文件下载到一个与桌面上的文件同名的文件夹中。目前,没有任何反应。

我有这些文件:

  • manifest.json(全部收集在一起)
  • popup.html(显示弹出窗口)
  • popup.js(等待按钮单击并将“downloadButton”消息发送到
  • content.js(找到所有链接,并触发下载,这允许
  • background.js(将它们下载到文件中)

但是,我现在得到的是弹出按钮出现,但没有发生下载。我在系统日志上看不到任何内容,并且有点不知道该去哪里查看。

这实际上是备份计划 - 我想从网页下载特定文件,但我不知道该怎么做,我研究发现这更容易,所以我想我应该先尝试了解它是如何工作的,然后尝试具体一个。

 {   
    "manifest_version": 2,
    "name": "Download Scraper",
    "version": "1.0",
    "permissions": ["activeTab", "downloads", "storage"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },

    "browser_action":{
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"],
            "run_at": "document_idle"
        }
    ]
    
}
<!DOCTYPE html>
<html>
<head>
  <title>File Downloader</title>
  <script src="popup.js"></script>
</head>
<body>
  <button id="downloadButton">Download File</button>
</body>
</html>
document.getElementById('downloadButton').addEventListener('click', function () {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      var tab = tabs[0];
      chrome.tabs.sendMessage(tab.id, { action: 'downloadFile' });
    });
  });
  
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
  if (message.action === 'downloadAllFiles') {
    // Find all links on the page
    var fileLinks = document.querySelectorAll('a');

    // Loop through each link and trigger a download
    fileLinks.forEach(function (link) {
      // Get the href attribute of the link (download URL)
      var downloadUrl = link.href;

      // Extract the file name from the href
      var fileName = getFileNameFromUrl(downloadUrl);

      // Send file information to the background script
      chrome.runtime.sendMessage({ action: 'initiateDownload', fileName: fileName, fileUrl: downloadUrl });
    });
  }
});

// Function to extract file name from a URL
function getFileNameFromUrl(url) {
  var matches = url.match(/\/([^\/?#]+)[^\/]*$/);
  return matches && matches.length > 1 ? decodeURIComponent(matches[1]) : null;
}

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === 'initiateDownload') {
    const fileName = message.fileName;
    const folderName = fileName.replace(/[^\w\s]/gi, ''); // Remove special characters from the title

    console.log('Folder Name:', folderName);

    const filePath = `Desktop/${folderName}/${fileName}.pdf`;
    chrome.downloads.download({
      url: message.fileUrl,
      filename: filePath
    }, () => {
      console.log('File downloaded and saved.');
    });
  }
});

javascript json google-chrome-extension plugins screen-scraping
© www.soinside.com 2019 - 2024. All rights reserved.