2012-12-01 79 views
3

我寫了一個簡單的html頁面,它有一個輸入和三個鏈接。Chrome擴展本地存儲不能正常工作

「OK」 - >店鋪無論是在提交到本地存儲

「WHOAMI」 - 與值>警報已保存到本地存儲

「檢查」 - >如果該值是「asd」,它回答「asd」或不回答「Asd」

非常簡單,我已經測試這只是一個普通的HTML頁面,它的工作原理。所有功能的行爲都應該如此。

這裏是index.html的

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> System Information </title> 
<script src="main.js"></script> 
</head> 
<body> 
<input type="text" name="text" id="text"> 
<a href="#" onclick="saveChanges(); return false">OK</a> 
<a href="#" onclick="who(); return false">WhoAmI</a> 
<a href="#" onclick="check(); return false">check</a> 

</body> 
</html> 

這裏是JavaScript main.js

function saveChanges() { 
    var theValue = text.value; 

localStorage["ChromeLogin"] = theValue; 
} 
function who() { 
    alert(localStorage["ChromeLogin"]); 
} 
function check(){ 
    var test = localStorage["ChromeLogin"]; 
    if (test == "asd"){ 
     alert("it is asd"); 
    }else{ 
     alert("It is NOT asd"); 
    } 
} 

enter image description here

然而,當我輸入完全相同的代碼,谷歌瀏覽器擴展,一切都停止工作。

這是我的manifest.json代碼。

{ 
    "name": "testLocalStorage", 
    "version": "0.0.1", 
    "description": "Capture a tab", 
    "options_page": "index.html", 

    "manifest_version": 2, 

    "browser_action": { 
    "default_title": "Capture tab" 
    }, 
    "permissions" : ["tabs", "<all_urls>", "storage"] 
} 

我假定這是問題: Issue Screen shot

我看了整個this但我無法弄清楚。

任何幫助將不勝感激

感謝

回答

3

看來你沒有完全瞭解link你提到:),有中引用的鏈接,它說inline script will not be executed.

inline script section對index.html進行的更改

1)刪除所有INLINE JS

<a href="#" onclick="saveChanges(); return false">OK</a>

<a href="#" onclick="who(); return false">WhoAmI</a>

<a href="#" onclick="check(); return false">check</a>

<a href="#" id="ok">OK</a>

<a href="#" id="who">WhoAmI</a>

<a href="#" id="check">check</a>

通過分配ID的

最終索引。HTML

<html> 
<head> 
<title> System Information </title> 
<script src="main.js"></script> 
</head> 
<body> 
<input type="text" name="text" id="text"> 
<a href="#" id="ok">OK</a> 
<a href="#" id="who">WhoAmI</a> 
<a href="#" id="check">check</a> 

</body> 
</html> 

作出main.js

1)添加的事件處理程序上的<a/>標籤點擊全部3個功能如下所示的變化

的window.onload =功能(){

document.getElementById('ok').onclick = saveChanges; 
document.getElementById('check').onclick = check; 
document.getElementById('who').onclick = who; 

}

最終main.js

function saveChanges() { 
    var theValue = text.value; 

    localStorage["ChromeLogin"] = theValue; 
} 
function who() { 
    alert(localStorage["ChromeLogin"]); 
} 
function check(){ 
    var test = localStorage["ChromeLogin"]; 
    if (test == "asd"){ 
     alert("it is asd"); 
    }else{ 
     alert("It is NOT asd"); 
    } 
} 
window.onload=function(){ 
    document.getElementById('ok').onclick=saveChanges; 
    document.getElementById('check').onclick=check; 
    document.getElementById('who').onclick=who; 
} 

變化作出的manifest.json在manifest.json

1)淘汰非必要的權限部最後的manifest.json

{ 
    "name": "testLocalStorage", 
    "version": "0.0.1", 
    "description": "Capture a tab", 
    "options_page": "index.html", 

    "manifest_version": 2, 

    "browser_action": { 
    "default_title": "Capture tab" 
    } 
} 

最終輸出

enter image description here

+2

謝謝你這麼多。這很好。現在我明白他們的意思是「不行」:D – Cripto

相關問題