18

I am developing a Chrome extension to tweak Facebook. However, catching browsing actions within HTML5-enabled sites such as Facebook requires an override of window.history.pushState as explained in this SO question.

Unfortunately, it seems that Chrome's isolated worlds prevent this sort of override. Is there any other method of catching history changes (other than polling document.location.href)?

1
  • i appreciate this is an old question, but on the off chance your extension is still active, what method did you decide to use in the end, and is it still working? i think the script injection method seems to be broken for facebook.com these days, possibly due to Facebook's CSP
    – user280109
    Mar 29 at 19:18

3 Answers 3

11

Not sure whether you trying to do this in background.js or content.js, but if it is the former, you can do so using webNavigation events:

You need to set permissions for webNavigation in manifest.json:

  "permissions": [
     "webNavigation"
  ],

Then in background.js:

  chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
        console.log('Page uses History API and we heard a pushSate/replaceState.');
        // do your thing
  });

Source: Chrome Extension docs for webNavigation

1
  • 3
    What if it in content.js? Do you still have to do a hack? May 7, 2015 at 21:40
7

Yes,

One way is to create a script tag and put your code there:

html = "window.history.pushState = function(a,b,c) { alert('Change !'); };";

var headID = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.innerHTML = html;
headID.appendChild(newScript);
5
  • what if you want to intercept the pushstate command, do something, and then let the pushstate command complete as normal, the example given above seems to intercept the pushstate fine, but it doesnt actually carry out the pushstate command afterwards, how would you implement that?
    – user280109
    Jan 23, 2011 at 7:44
  • found a solution here, not sure if it works with chrome yet though : stackoverflow.com/questions/4570093/…
    – user280109
    Jan 23, 2011 at 8:32
  • Has this stopped working since the answer was posted, or is there some permission etc that is left out...? Jun 6, 2015 at 7:15
  • 1
    This doesn't work. Chrome Extensions (content scripts) do not have access to the variables scope of the page, although they have access to the DOM.
    – fiatjaf
    Mar 13, 2016 at 2:32
  • i dont think this method works any more (at least it doesn't seem to on facebook.com possibly due to facebook;s CSP? )
    – user280109
    Mar 29 at 19:15
4

Building on @Boris's answer: to continue with the default action, save a reference to the original pushState function, and then call it:

var headID = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = chrome.extension.getURL('script.js');
headID.appendChild(newScript);

script.js:

window.history.pushState = (function(nativePushState) {
    return function(a,b,c) {
        // Do something
        nativePushState.apply(this, arguments); //Continue by calling native history.pushState
    };
})(window.history.pushState)
1
  • i dont think this method works any more (at least it doesn't seem to on facebook.com possibly due to facebook;s CSP? )
    – user280109
    Mar 29 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.