简体   繁体   中英

History search failing to execute in Chrome extension

I am in the process of writing an extension for Chrome to display the users 3 most visited sites. (Yes, I am aware that the "New Tab" page already does this) However, whenever I try to query the users history then it seems like the entire script shuts down.

My manifest files does contain:

{
    "name": "Most Visited Sites Test",
    "description": "Show your most visited sites",
    "version": "1.0",
    "background_page": "background.html",
    "app": {
        "launch": {
            "web_url": "http://localhost/*"
        }
    },
    "permissions": [
        "tabs",
        "history",
        "unlimitedStorage",
        "notifications"
    ],
    "icons": {"128": "icon.png" },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["contentscript.js"]
        }
    ]
}

So I believe this ought to give my background page the ability to use the history. However, my background page contains:

function onRequest(request, sender, sendResponse)
{
    alert("Call 1");

    var oneWeekAgo = //Code for getting last weeks date;
    chrome.history.search({
    'text': '',
    'startTime': oneWeekAgo
    },
    function(historyItems)
    {
        // Do stuff...
    });

   alert("Call 2");
};

The request is sent from my contentscript.js

chrome.extension.sendRequest("foo");

When run, "Call 1" is shown but then nothing is done with the history and "Call 2" is never shown. What might be causing this? I apologize if this is a simple problem but this is my first attempt at a legitimate Chrome extension.

Opening console to see if there any errors is the first thing I always do (go to the Extensions tab and click on "background.html").

Your history call is correct, so maybe your last week calculation isn't? This is what works for me:

chrome.history.search({text: "", startTime:(new Date()).getTime()-7*24*3600*1000}, function(items) {
    console.log(items);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM