1

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

mohamed
  • 231
  • 1
  • 13
  • This means "chrome.storage.local" doesn't have a value and is `undefined`. You need to figure out why that is. It's not a problem with the parameters you are feeding into `get` or `set`, but rather "chrome.storage.local" itself. – computercarguy Dec 16 '21 at 16:02
  • Does this answer your question? [How can I save information locally in my chrome extension?](https://stackoverflow.com/questions/5364062/how-can-i-save-information-locally-in-my-chrome-extension) – computercarguy Dec 16 '21 at 16:02
  • @computercarguy, it didn't solve the problem because chrome.storage.local always gives error, i tried it in its simplest form with key and value only and still gives error . the parameters are correct as i have already tested them. Everything in the code runs good except the function itself – mohamed Dec 16 '21 at 16:20
  • So, going back to my first comment, "chrome.storage.local" doesn't have a value so it doesn't have the object that would contain the `get` and `set` functions. You can verify that by outputting it to the console. I haven't used it before, so IDK how to make it have a setting. And none of my quick searching are bringing up anything, either. – computercarguy Dec 16 '21 at 16:35
  • @computercarguy, I tried using `sessionstorage` and it worked but this makes data highly vulnerable to loss. Do you have a better idea to just send the variable – mohamed Dec 16 '21 at 16:43
  • @computercarguy, i used sessionStorage as a temporary solution (i will just use `locastorage` in the future`) . no errors were discovered but now the extension doesn't do what it is supposed to do – mohamed Dec 16 '21 at 16:59
  • 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. – computercarguy Dec 16 '21 at 17:41
  • @computercarguy, it solved it . Nice observation and thanks for help. i have another question about the same code (i want the code to just rename the file and download without opening the save as dialogue). should i close the question and make another one for that or just wait? – mohamed Dec 16 '21 at 18:35
  • That would have to be another question. It's not allowed to tack on a 2nd unrelated question to an existing one, even if it's your own question. BTW, since my comment solved your issue, I'm going to move it to an answer so you can accept it. This helps with how your question is ranked as well as preventing it from getting automatically deleted in a month or so. – computercarguy Dec 16 '21 at 18:37

1 Answers1

2

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.

computercarguy
  • 2,173
  • 1
  • 13
  • 27