2017-06-26 65 views
0

這裏就是我需要幫助的代碼:我試圖做一個Chrome擴展設置頁,它不工作

if (localStorage.KillTabs == undefined) { 
 
    localStorage.KillTabs = 1; 
 
}; 
 
if (document.getElementById('killer')) { 
 
    if document.getElementById('killer').checked = true { 
 
    localStorage.KillTabs = 0; 
 
    } 
 
} 
 
if (localStorage.KillTabs == 1) { 
 
    console.log("Cannot Clear Tabs"); 
 
} else { 
 
    console.log("Clearing Tabs...") 
 
    //im going to put something here when i know that this works 
 
} 
 
}
<html> 
 

 
<head> 
 
    <script src="options.js"></script> 
 
</head> 
 

 
<body> 
 
    <h1>PrivacyPro Options</h1> 
 
    <input type="checkbox" id="killer"> 
 
    <p class="label">Close All Tabs when done</p> 
 
</body> 
 

 
</html>

我覺得我做了正確的,但我不確定。我沒有看到任何錯誤。我還希望將設置保存爲當複選框被選中時我希望它保存,所以每次運行擴展時,該設置都會激活,並且只有在複選框未選中時纔會激活。任何幫助都會很好,謝謝!

+0

使用https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer和https://developer.chrome.com/extensions/storage –

+0

那些示例令我困惑 – joesmalls23

+0

谷歌瀏覽器示例的字面意思正是你要做的,但正確使用'chrome.storage',而不是'localStorage':https://developer.chrome.com/extensions/optionsV2 – Makyen

回答

0

您錯誤地使用了接口storage。這是chrome.storage不是localStorage(即使在瀏覽器中它將是localstorage,全部小寫)。

爲了使用storage接口,您需要確保您在manifest.json文件中請求存儲權限。

"permissions": [ 
    "storage" 
], 

接下來,你需要使用存儲對象get()set()方法來獲取和設置基於一個鍵值。這也使用回調機制,所以你不能通過調用get()來獲取值。

chrome.storage.local.get('killtabs', function(result) { 
    if (result === 1) { 
    // do something 
    } 
}); 
相關問題