2

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!

Dan
  • 2,299
  • 2
  • 20
  • 41
  • In general, the appAPI.dom.onDocumentStart.addJS method runs at the earliest opportunity for each browser. However, to try and assist you further I need more information such as (1) Which Operating System are you testing on (2) Which browser are you testing on (3) Sample code (4) Steps for reproducing the problem. [Disclosure: I am a Crossrider employee] – Shlomo Feb 23 '14 at 12:26
  • Thanks for the reply Shlomo, I edited my question with all the additional info you requested. – Dan Feb 23 '14 at 19:43

1 Answers1

2

@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]

Shlomo
  • 3,763
  • 11
  • 16
  • You don't need to trigger the function. It starts automatically as it's part of the background code. – Shlomo Jun 17 '15 at 14:18