• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Auto-Downloder Chrome Extension

    September 18th, 2021
    comments, tech
    Let's say you want Chrome to automatically download pieces of pages, such as Facebook comments. How could you do it? I recently wanted to do this and couldn't find docs, so here's what I did.

    1. Make a folder somewhere for a browser extension.

    2. In that folder, make manifest.json with contents like:

      {
        "name": "Downloader",
        "description": "Downloads stuff",
        "version": "1.0",
        "manifest_version": 3,
        "permissions": [
          "activeTab",
          "downloads"
        ],
        "background": {
          "service_worker": "background.js"
        },
        "content_scripts": [{
          "matches": [
            "https://example.com/path/*",
          ],
          "js": ["content_script.js"]
        }]
      }
      
    3. In content_script.js put:

      // This file extracts what you want from the page
      // and asks the background script to save it.
      
      // You write the code to extract what you want.
      const yourStringToSave =
        yourFunctionToExtractFromPage();
      
      chrome.runtime.sendMessage(
          /* extension id not needed */ undefined,
          [fileNameToUse,
           yourStringToSave]);
      
    4. In background.js put:

      // This file receives messages from the content
      // script and puts them in your Dowloads folder.
      
      function makeDataUrl(body) {
        // We use a data: url because Chrome has
        // trouble with object URLs in Incognito.
        return "data:application/json;base64," +
          btoa(unescape(encodeURIComponent(body)));
      }
      
      chrome.runtime.onMessage.addListener(
        function(message) {
          const fname = message[0];
          const body = message[1];
      
          chrome.downloads.download({
            conflictAction: "overwrite",
            filename: fname,
            url: makeDataUrl(body),
          });
        });
      
    5. Visit chrome://extensions

    6. Enable Developer Mode

    7. Click the "Load unpacked" and select the extension directory

    8. If you need it in Incognito, click "Details" on the extension card and then enable "Allow in Incognito".

    Example code: github.

    (And, yes, this is how Facebook comments are back.)

    Comment via: facebook, lesswrong

    Recent posts on blogs I like:

    Be less scared of overconfidence

    deferring to markets • deferring to experts • deferring to low-information heuristics • why they fail • blindness to outliers • what to do instead

    via benkuhn.net November 30, 2022

    Corncob Dolls

    I went to a farm and at the farm I got to see a corncrib and the corn that had fell out of the corncrib that no one wanted I got to use my fingers to take off the corn kernels and once the cobs were empty I put them in a bag and then once I got back to the…

    via Anna Wise's Blog Posts November 7, 2022

    Light Switch

    When I got my loft bed it was just so annoying every morning to have to get out of bed, climb down the ladder, turn the light on, and climb back up, just so I could see stuff. I decided to make a string for my light switch because I really wanted to be abl…

    via Lily Wise's Blog Posts November 7, 2022

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact