Abh*_*hek 1 javascript google-chrome google-chrome-extension
我试图检查是否已在chrome.storage.local中设置了键值对.因为我在这里采取了类似的问题的帮助.这里我试图从存储中获取的变量尚未设置.当我尝试使用lastError捕获错误.它是未定义的,而假设由于读取先前未设置的变量(根据我用googled)的错误而定义和设置.请在读取尚未设置的变量时帮助我检测错误.
的manifest.json
{
"name": "TestExtension",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["jquery.js", "background.js"]
},
"description": "Test the extension features here",
"icons": {
"16": "images/16x16.png",
"48": "images/48x48.png",
"128": "images/128x128.png"
},
"page_action": {
"default_icon": {
"19": "images/19x19.png",
"38": "images/38x38.png"
},
"default_title": "Test Plugin default title",
"default_popup": "popup.html"
},
"permissions": ["tabs", "storage", "http://*/*", "https://*/*"]
}
Run Code Online (Sandbox Code Playgroud)
background.js
function showTheExtension(tabId, changeInfo, tab) {
console.log("Extension loaded");
chrome.pageAction.show(tabId);
} // ---EOF checkForValidUrl---
chrome.tabs.onUpdated.addListener(showTheExtension);
Run Code Online (Sandbox Code Playgroud)
popup.js
$('document').ready(function() {
chrome.storage.local.get('testVar1', function(result) {
if (chrome.runtime.lastError) {
alert('Var not set')
}
console.log(result);
console.log(chrome.runtime.lastError.message);
});
});
Run Code Online (Sandbox Code Playgroud)
popup.html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h3>Welcome to the test project for designing chrome plugins</h3>
<script type="application/x-javascript" src="./jquery.js"></script>
<script type="application/x-javascript" src="./popup.js"></script>
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
不存在该值不是API错误,因此chrome.runtime.lastError
未设置.
如果您想知道变量是否设置,请检查结果:
chrome.storage.local.get('testVar1', function(result) {
if (result.testVar1 === undefined) {
alert('Var not set');
return;
}
// Rest of code...
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3130 次 |
最近记录: |