(JSON) Trying to make a scraping tool to download files from a website

fairly new to this area, and not really sure what I’m doing, so some help would be appreciated.

I’ve been trying to build a plugin for Chrome that will download all files on a page, into a folder that is named the same as the file on the desktop. Currently, nothing happens.

I’ve got these files:

  • manifest.json (collects it all together)
  • popup.html (displays the popup)
  • popup.js (waits for the button click and sends the ‘downloadButton’ message to
  • content.js (which finds all the links, and triggers the download, which allows
  • background.js (to download them into the file)

However, what I’m getting at the moment is the pop-up button coming up, but no downloads happening. I can’t see anything on system logs, and am a bit lost on where to look.

This is actually the backup plan – I want to download specific files from webpages but I dont’ know how to do that and I researched and found this was easier so I thought I’d try to understand how this works first and then try the specific one.

I’ve attached my code (it’s not toooooo long). Not sure how general standards and protocols work on here. Sorry for any stackoverflow mishaps!

 {   
    "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(/[^ws]/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.');
    });
  }
});