0

I'm trying to write an extension to delete some URLs from my history when they are navigated to. Here are the relevant files -

manifest.json

{
    "manifest_version": 2,

    "name": "Secret",
    "description": "Browser History Edits.",
    "version": "0.1",

    "browser_action": {
    },

    "background": {
        "scripts": ["background.js"]
    },

    "permissions": [
        "webNavigation",
        "history",
        "tabs"
    ]
}

background.js

var prevent_logging_keywords = ["reddit", "stackoverflow"]

chrome.webNavigation.onBeforeNavigate.addListener(function(details) {
    var currentUrl = details.url;

    prevent_logging_keywords.forEach(function(keyword) {
        if (currentUrl.includes(keyword)) {      
            console.log(currentUrl);
            chrome.history.deleteUrl({ "url": currentUrl}, function(){});
        }
    });
});

However, this does not work. The URL still shows up in my history and, what's more, the console.log is also never called. What am I doing wrong?

fwx
  • 333
  • 1
  • 5
  • 14
  • 2
    I guess 1) you're looking at the [wrong console](https://stackoverflow.com/a/10258029), and 2) onBeforeNavigate event seems a bad choice since an URL must be added to history after successful navigation, not before. – wOxxOm Sep 17 '16 at 07:00
  • so any suggestions as to how i can make it work i.e. what event can i use to ensure that the url is indeed deleted? I want to make it work in the background.js everytime a new tab is opened. I don't want to use a popup.js which will respond after I take the effort to click on it. – fwx Sep 17 '16 at 07:06
  • The order of `webNavigation` events is `onBeforeNavigate` ➞ `onCommitted`➞ `onDOMContentLoaded` ➞ `onCompleted`. `onCompleted` appears to be the obvious choice, but `onDOMContentLoaded` might also work. – Makyen Sep 17 '16 at 07:11
  • However, it appears that [`chrome.history.onVisited`](https://developer.chrome.com/extensions/history#event-onVisited) would be a more appropriate event to be listening to as that is the event that fires when the [historyItem](https://developer.chrome.com/extensions/history#type-HistoryItem) is visited (i.e. the URL is added to the history). – Makyen Sep 17 '16 at 07:18
  • Thanks! I tried out all your suggestions. For some reason, only `onCompleted` works as expected. Oh well, the problem is solved nevertheless. – fwx Sep 17 '16 at 07:39
  • On a related note, what is the recommended way to store the prevent_logging_keywords array to a file / local storage? So that I can separate the data and processing concerns and also access this data from other scripts I have in the extension. – fwx Sep 17 '16 at 18:42
  • 1
    [chrome.storage.local, or chrome.storage.sync.](https://developer.chrome.com/extensions/storage) – Makyen Sep 18 '16 at 00:20

0 Answers0