删除历史记录chrome扩展

时间:2017-01-20 21:03:56

标签: javascript json google-chrome google-chrome-extension popup

我正在尝试创建一个简单的Chrome扩展程序,用于删除所选的“浏览历史记录”属性。我已经学习了几个教程,并且尽管我不熟悉JS,JSON和HTML,但我还是尽量保持编程的简单性。此外,由于我无法在任何地方找到编译我的代码,因此很难确定我的错误。任何人都可以看看我的代码,并指出我有什么问题吗?我只是想学习,所以我正在寻找的是一些解释。但是,任何事都有帮助这是我的弹出窗口和清单:

的manifest.json:

{

"name": "1-Click Clear",
"description": "1 click and your Chrome history is cleared!",
"version": "1.0",

"manifest_version": 2,

"browser_action":{
"default_icon": "icon.png",
"default_popup": "popup.html"
},

"permissions":[
    "history",
    "topSites",
    "browsingData"
]



}

popup.html:

<!DOCTYPE html>
<html>
<body>

<form>
  <input type="checkbox" name="history" value="male"> Delete History<br>
  <input type="checkbox" name="cookies" value="female"> Delete Cookies<br>
  <input type="checkbox" name="cache" value="other"> Delete Cache <br>  
  <input type="checkbox" name="passwords" value="female"> Delete Passwords <br>
</form> 
<br>

<button type = "submit" id = "all"> <b>Clear All Above</b></button>
 <button type = "submit" id = "chosen"> <b>Clear Chosen</b></button>



</body>
</html>

popup.js:

var callback = function(){};
var millisecondsPerMonth = 1000 * 60 * 60 * 24 * 7 * 4;

var oneMonthAgo = (new Date()).getTime() - millisecondsPerMonth;

function clear()
{
    if(document.getElementsByName("history").checked)
    {
        chrome.browsingData.removeHistory("since": oneMonthAgo,callback);
    }
    if(document.getElementsByName("cookies").checked)
    {
        chrome.browsingData.removeCookies("since": oneMonthAgo,callback);
    }
    if(document.getElementsByName("cache").checked)
    {
        chrome.browsingData.removeCache("since": oneMonthAgo,callback);
    }
    if(document.getElementsByName("passwords").checked)
    {
        chrome.browsingData.removePasswords("since": oneMonthAgo,callback);
    }
}

function clearAll()
{
    chrome.browsingData.removeHistory("since": oneMonthAgo,callback);
    chrome.browsingData.removeCookies("since": oneMonthAgo,callback);
    chrome.browsingData.removeCache("since": oneMonthAgo,callback);
    chrome.browsingData.removePasswords("since": oneMonthAgo,callback);
}


document.getElementById('chosen').addEventListener('click',clear);
document.getElementById('all').addEventListener('click',clearAll);

EDITED popup.js:

var callback = function(){};
var millisecondsPerMonth = 1000 * 60 * 60 * 24 * 7 * 4;

var oneMonthAgo = (new Date()).getTime() - millisecondsPerMonth;

function clear()
{
    if(document.getElementsByName("history").checked)
    {
    chrome.browsingData.removeHistory({"since": oneMonthAgo}, callback);
    }
    if(document.getElementsByName("cookies").checked)
    {
         chrome.browsingData.removeCookies({"since": oneMonthAgo}, callback);
    }
    if(document.getElementsByName("cache").checked)
    {
        chrome.browsingData.removeCache({"since": oneMonthAgo}, callback);
    }
    if(document.getElementsByName("passwords").checked)
    {
        chrome.browsingData.removePasswords({"since": oneMonthAgo}, callback);
    }
}

function clearAll()
{
    chrome.browsingData.removeHistory({"since": oneMonthAgo}, callback);
chrome.browsingData.removeCookies({"since": oneMonthAgo}, callback);
    chrome.browsingData.removeCache({"since": oneMonthAgo}, callback);
chrome.browsingData.removePasswords({"since": oneMonthAgo}, callback);
}


var doc = document.getElementById('chosen');
if(doc)
{
    doc.addEventListener('click',clear);
}
var doc = document.getElementById('all');
if(doc)
{
    doc.addEventListener('click',clearAll);
}

EDITED popup.html:

<!DOCTYPE html>
<html>
<body>
<script src="popup.js"></script>
<form>
  <input type="checkbox" name="history" value="male"> Delete History<br>
  <input type="checkbox" name="cookies" value="female"> Delete Cookies<br>
  <input type="checkbox" name="cache" value="other"> Delete Cache <br>  
  <input type="checkbox" name="passwords" value="female"> Delete Passwords <br>
</form> 
<br>

<button type = "submit" id = "all"> <b>Clear All Above</b></button>
 <button type = "submit" id = "chosen"> <b>Clear Chosen</b></button>



</body>
</html>

0 个答案:

没有答案