Cannot delete URLs with Chrome's history API

為{幸葍}努か 提交于 2019-12-13 06:13:36

问题


I am making a Chrome extension to remove certain URLs from my history, using Chrome's history API. Here is the code so far:

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 is the form in my popup, and query is the text box in which you can type.)

As you can see from the three alerts, the variables are fine. However, the code isn't actually removing the URL from the history. When I check (in chrome://history/), the URLs are still there.

Here is my manifest.json, if that matters:

{
    "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"
    }
}

I am using version 28.0.1500.95 (Official Build 213514) m of Chrome.


回答1:


You should not pass a string to the chrome.history.deleteUrl method, but an object with key url. If you inspect your popup, you would see the following error:

Error: Invocation of form history.deleteUrl(string) doesn't match definition history.deleteUrl(object details, optional function callback)

In summary, change

chrome.history.deleteUrl(query.value)

to

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


来源:https://stackoverflow.com/questions/18363171/cannot-delete-urls-with-chromes-history-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!