Ad

In Newer Versions Of Firefox, Is It Still Possible To Override A Web Page's JS Function?

I am writing an extension to override a web page's JS function, and started from this question, but the answer does not appear to work in Firefox 42 on Linux.

Next, I tried to use exportFunction as described in the documentation, but that also silently failed.

Inside package.json, I have added the following sesction.

  "permissions": {
      "unsafe-content-script": true
  }

Here is my index.js file.

var self = require('sdk/self');
require("sdk/tabs").on("ready", fixGoogle);

function fixGoogle(tab) {
    if (tab.url.indexOf("google.com") > -1) {
        tab.attach({
            contentScriptFile: self.data.url("google-script.js")
        });
    } 
}

Here is my current data/google-script.js.

unsafeWindow.rwt=function(){};

Note that manually typing in rwt=function(){}; to the browser's console achieves the desired effect, as does using a bookmarklet (which requires clicking) but I am writing the plugin to get this automatically every time I use Google.

Is it possible to override the rwt page function using a Firefox extension? If so, what is the correct API to use?

Ad

Answer

It turns out that the issue is that the redefinition of the function rwt is racing against the original definition and winning. The original runs after and overrides the function I defined, thereby making it look like my redefinition had silently failed.

Once I realized that this was the problem, the easiest hack around it was to add a timeout to the redefinition inside data/google-script.js.

setTimeout(function() {
    unsafeWindow.rwt=function(){};
}, 1000);

Thus, the orignal answer is still correct but simply failed to address the race condition.

Ad
source: stackoverflow.com
Ad