rohanp
rohanp

Reputation: 630

Chrome History API bizarre behavior

I am trying to create a simple chrome extension to delete a reddit url from my history every time I go to one and then replace it with a random wikipedia article. To do this I have a content script that passes a message to a background script to delete its url from the history:

content_script.js:

window.onload = chrome.runtime.sendMessage({greeting: "do the thing"});

background.js:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {

    console.log("deleting url " + sender.tab.url);
    //chrome.browsingData.removeCache({});

    chrome.history.deleteUrl( {url: sender.tab.url}, 
        function () {
            console.log("replacing url");
            chrome.history.addUrl({url: "https://en.wikipedia.org/wiki/Special:Random"});
        } 
    );
 });

All my console.log prints confirm that my script is executing correctly, no errors are thrown (even on the generated background page) but when I look at my history from chrome://history all the reddit urls I visited are still there, and there is just a single wikipedia visit at the top. (image)

The bug is interesting because when I check the history using the chrome history API the reddit links aren't there! All that is printed is a single wikipedia link and my non-reddit history.

function printHistory(){
        chrome.history.search({text: '', maxResults: 10},

         function(data) {
            data.forEach(function(page) {
                console.log(page.url);
                });
        });
    } 

So there are three problems

  1. reddit urls aren't being deleted in chrome://history
  2. wikipedia urls are only added once, at the end (or are somehow deleted and re-added each time).
  3. https://en.wikipedia.org/wiki/Special:Random isn't being resolved to an real link

Heres a git repo you can clone to reproduce the bug. https://github.com/rohanp/test

I have Chrome Version 44.0.2403.125 (64-bit)

Upvotes: 1

Views: 338

Answers (1)

Xan
Xan

Reputation: 77601

1) reddit urls aren't being deleted in chrome://history

This is a known bug due to History Sync not being affected by delete. It's unclear when it will be fixed.

2) wikipedia urls are only added once, at the end (or are somehow deleted and re-added each time)

That's just how History works - it only shows you the last visit to a particular page, hiding the previous ones.

3) https://en.wikipedia.org/wiki/Special:Random isn't being resolved to an real link

And why would it? It's not like you're really visiting the page.

You should make an XHR request to Wikipeida API to get a random page.

This should work, parse the result as JSON: https://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&format=json

Upvotes: 3

Related Questions