【问题标题】:problem with extension intercepting downloads扩展拦截下载的问题
【发布时间】:2022-01-19 17:00:24
【问题描述】:

我正在尝试编写一个扩展程序来拦截下载并重命名它们

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;
    }
  });

我有两个问题:

  1. 控制台给出了两个错误,特别是在chrome.storage.local.setchrome.storage.local.get 它说Uncaught TypeError: Cannot read properties of undefined (reading 'local') 我尝试只在控制台中使用chrome.storage.local.set({[document.URL]: "hi"}) 运行代码,但仍然给出错误

  2. 我知道我使用了suggest,但我希望扩展名只是重命名文件而无需我按下弹出窗口

【问题讨论】:

  • 这意味着“chrome.storage.local”没有值并且是undefined。你需要弄清楚为什么会这样。这不是您输入getset 的参数的问题,而是“chrome.storage.local”本身的问题。
  • @computercarguy,它没有解决问题,因为 chrome.storage.local 总是给出错误,我以最简单的形式尝试了它,只有键和值,但仍然给出错误。参数是正确的,因为我已经测试过它们。除了函数本身之外,代码中的所有内容都运行良好
  • 所以,回到我的第一条评论,“chrome.storage.local”没有值,因此它没有包含getset 函数的对象.您可以通过将其输出到控制台来验证这一点。我以前没用过,所以IDK如何让它有一个设置。我的快速搜索也没有找到任何东西。
  • @computercarguy,我尝试使用 sessionstorage 并且它有效,但这使得数据极易丢失。你有更好的主意来发送变量

标签: javascript jquery json google-chrome google-chrome-extension


【解决方案1】:

将我的观察转移到答案,因为它有效:

我刚刚注意到您的 manifest.json 中有 2 个“权限”部分。第二个可能会覆盖第一个。尝试将它们组合起来,看看是否可行。

【讨论】:

    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多