Question

I am playing around with building some browser extensions with Crossrider. I need some code to execute before pageload, before any DOM elements are rendered. Is this possible with Crossrider?

I have tried using appAPI.dom.onDocumentStart.addJS in the background scope but with no luck. I still see the page partially display before my code executes.

Thanks

UPDATE with More Details

An example of what I'm trying to do is an extension that redirects bad websites to good ones. Basically if a user hits a website that is bad on our list it will redirect them to a good alternative and give them a message. I am working on the redirect part and here is some sample code.

Inside my background.js

appAPI.ready(function($) {
   appAPI.dom.onDocumentStart.addJS({resourcePath:'main.js'});

});

Then I have a main.js file in my Resources

if(window.location.href == "http://www.bing.com/"){
    console.log("You are at Bing... That's bad m'kay.");
    window.location = "http://www.google.com";
} 

In this case if a user goes to bing they get redirected to google and then would get some kinda funny message about it. They will also get warnings for sites like 4chan, torrent sites etc.

So currently if I implement this code, and go to bing it flashes the DOM and then redirects. I am trying to prevent this. Thanks!

I am testing on Win 7 and Chrome

Thanks!

Was it helpful?

Solution

@Dan Thanks for providing the additional info, in particular the scenario that you are trying to achieve.

In this instance, you can achieve your goal by simply using the onBeforeNavigate method without the need to run at document start. So, based on your background.js example code, the solution would look like:

appAPI.ready(function() {
  var resourceArray = ['http://www.bing.com/'];
  appAPI.webRequest.onBeforeNavigate.addListener(function(details, opaqueData) {
    // Where:
    //   * details.pageUrl is the URL of the tab requesting the page
    //   * opaqueData is the data passed to the context of the callback function

    // Redirect requests for blocked pages
    for (var i = 0; i < opaqueData.length; ++i) {
      if (details.pageUrl.indexOf(opaqueData[i]) !== -1) {
        console.log("You are at Bing... That's bad m'kay.");
        return { redirectTo: 'http://www.google.com' };
      }
    }
  }, resourceArray); // resourceArray is the opaque parameter that is passed to the callback function
});

[Disclosure: I am a Crossrider employee]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top