无法使用Chrome'删除URL;的历史API

Cannot delete URLs with Chrome's history API

本文关键字:删除 URL 历史 API Chrome      更新时间:2023-09-26

我正在使用Chrome的历史API进行Chrome扩展,以从我的历史记录中删除某些URL。这是迄今为止的代码:

document.addEventListener('DOMContentLoaded', function() {
    var form = document.getElementById('form'),
        query = document.getElementById('query')
    form.onsubmit = function(e) {
        e.preventDefault()
        // alert(chrome.history)            // [object Object]
        // alert(chrome.history.deleteUrl)  // function () { [native code] }
        // alert(query.value)               // whatever I typed
        chrome.history.deleteUrl(query.value)
    }
    query.focus()
})

form是我弹出窗口中的表单,query是您可以在其中键入的文本框。)

正如您从三个alert中看到的那样,这些变量都很好。然而,代码实际上并没有从历史记录中删除URL。当我检查(在chrome://history/中)时,URL仍然存在。

这是我的manifest.json,如果重要的话:

{
    "manifest_version": 2,
    "name": "UnVizit",
    "description": "Mark a link as '"not visited'" easily",
    "version": "0.1.0",
    "permissions": [
        "history"
    ],
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    }
}

我使用的是Chrome的28.0.1500.95 (Official Build 213514) m版本。

您不应该将字符串传递给chrome.history.deleteUrl方法,而应该将一个具有键url的对象传递给它。如果你检查弹出窗口,你会看到以下错误:

错误:调用表单历史记录.deleteUrl(字符串)与定义历史记录不匹配。deleteUrl(对象详细信息,可选函数回调)

总之,更改

chrome.history.deleteUrl(query.value)

chrome.history.deleteUrl({ url: query.value });