Home > database >  problem with extension intercepting downloads
problem with extension intercepting downloads

Time:12-17

i am trying to program an extension to intercept downloads and rename them

manifest.json:

{
    "name": " Ebooks Downloader",
    "description": "Automatically rename ebooks downloaded from gutenberg.org",
    "version": "1.0",
    "author": "",
    "manifest_version": 2,
    
    "content_scripts": [
        {
        "matches": ["https://gutenberg.org/ebooks/*"],
        "js": ["content_script.js"]
        }
    ],

    "permissions": [
        "https://gutenberg.org/*",
        "storage"
    ],
    
   "background": {
        "scripts": ["background.js"],
        "persistent": false
    },

    "permissions": [
        "downloads"
    ]
}

content_script.js :

// Get the content of the h1 title
var nameProp = document.querySelector('[itemprop=name]').textContent;

// Set everything to lower case, remove special characters and standardize format
nameProp = nameProp.toLowerCase().replace(/[^a-z0-9 ]/gi, '');
var filename = nameProp.replace(' by ', ' - '); 

// use the storage API 
chrome.storage.local.set({[document.URL]: filename}, function() {
    console.log('Book filename is stored as: '   filename);
});

background.js:

chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
    if (item.referrer.search("gutenberg.org") == -1) {
    // If the file does not come from gutenberg.org, suggest nothing new.
        suggest({filename: item.filename});
    } else {
    // Otherwise, fetch the book's title in storage...
        chrome.storage.local.get([item.referrer], function(result) {
            if (result[item.referrer] == null) {
                // ...and if we find don't find it, suggest nothing new.
                suggest({filename: item.filename});
                console.log('Nothing done.');
            }
            else {
                // ...if we find it, suggest it.
                fileExt = item.filename.split('.').pop();
                var newFilename = "gutenberg/"   result[item.referrer]   "."   fileExt;
                suggest({filename: newFilename});
                console.log('New filename: '   newFilename);
            }
          });
        // Storage API is asynchronous so we need to return true
        return true;
    }
  });

I have two problems:

  1. the console gives two errors particularly at chrome.storage.local.set and chrome.storage.local.get it says Uncaught TypeError: Cannot read properties of undefined (reading 'local') i tried running the code only with chrome.storage.local.set({[document.URL]: "hi"}) in console and still gave error

  2. i know that i used suggest but i want the extension to just rename the file without having me to press the pop-up

CodePudding user response:

Moving my observation to an answer, since it worked:

I just noticed that you have 2 "permissions" sections in your manifest.json. It's possible the 2nd one is overriding the first one. Try combining them and see if that works.

  • Related