我正在使用Chrome扩展程序并尝试访问存储API。但是我收到了一个控制台错误:
notes.js:25 Uncaught TypeError:无法读取属性' sync'未定义的 在HTMLTextAreaElement.save_notes(notes.js:25)
注释掉storage.sync调用并取消注释privacy.services调用(严格来测试第二个Chrome API)我得到了这个错误:
notes.js:30 Uncaught TypeError:无法读取属性' services'的 未定义HTMLTextAreaElement.save_notes(notes.js:30)
任何人都知道哪些无法访问manifest.json权限中明确列出的任何Chrome API可能是一个标志?
这是 javascript 文件。在测试中,错误发生在" save_notes"功能调用。
console.log("note.js connected")
///// select the text area
var note_area = document.getElementById('notetext')
//// todo Create a function to save the textarea to local storage
// todo First check & load any previously saved notes
;(function load_notes() {
if (note_area === "") {
chrome.storage.sync.get("stored_obj", function(resultObj) {
note_area.value = resultObj.stored_obj
console.log("Attempted to load notes into a blank note area. Did it run correctly?")
})
}
})()
function save_notes () {
chrome.storage.sync.set({
stored_obj: note_area.value
}, function () {
console.log("Saved into Chrome Storage")
})
// chrome.privacy.services.autofillEnabled.get({}, function(details) {
// if (details.value)
// console.log('Autofill is on!');
// else
// console.log('Autofill is off!');
//});
}
note_area.addEventListener('blur', save_notes)
//// setup a blur handler in notes.html to fire function save_notes
// could not be done (inline JS). I need to add a listener and a function in this file
function console_fire() {
console.log ('fired the console log')
}
这是 manifest.json 文件:
{
"manifest_version": 2,
"name": "Mindless Blocker",
"version": "0.1",
"description": "Block mindless distractions and have a place to take notes for followup during free time",
"browser_action": {
"default_icon": "cloud-icon.png",
"default_popup": "index.html"
},
"permissions": [
"storage",
"privacy"
]
}