我正在嘗試撰寫一個擴展程式來攔截下載并重命名它們
清單.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);
});
背景.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;
}
});
我有兩個問題:
控制臺給出了兩個錯誤,特別是在
chrome.storage.local.set,chrome.storage.local.get它說Uncaught TypeError: Cannot read properties of undefined (reading 'local')我嘗試只chrome.storage.local.set({[document.URL]: "hi"})在控制臺中運行代碼,但仍然給出錯誤我知道我使用過,
suggest但我希望擴展名只是重命名檔案,而不必按彈出視窗
uj5u.com熱心網友回復:
將我的觀察移至答案,因為它有效:
我剛剛注意到您的 manifest.json 中有 2 個“權限”部分。第二個可能會覆寫第一個。嘗試組合它們,看看是否有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/384256.html
標籤:javascript 查询 json 谷歌浏览器 谷歌浏览器扩展
上一篇:jQuery缺少)在引數串列之后,但()和閉合和轉義引號不起作用
下一篇:在影片中洗掉()不會洗掉
