3

I am developing a Firefox Mobile (Fennec) extension and I want to use jQuery in the content script. What is the best way to do it?

I am doing the testing on the desktop version of Firefox Mobile 4

Nickolay
  • 31,095
  • 13
  • 107
  • 185
benkol
  • 351
  • 1
  • 13
  • are you using the add-on SDK? Why are you asking this question? If you already have a content script you know you can load jquery like in this example: https://addons.mozilla.org/en-US/developers/docs/sdk/1.2/dev-guide/addon-development/content-scripts/reddit-example.html – Nickolay Dec 04 '11 at 20:47
  • 1
    I don't use add-on SDK, As far as I know there is no add-on SDK for Firefox Mobile (Fennec). – benkol Dec 05 '11 at 10:21
  • @benkol There is a pull request on github for porting it to mobile: https://github.com/mozilla/addon-sdk/pull/245. It is very experimental. Please let me know if you manage to get it working. – BenoitParis Dec 05 '11 at 11:33
  • @BenoitParis I'm sure there is an easier way. – benkol Dec 05 '11 at 16:41

1 Answers1

2

overlay.js

window.addEventListener("load", function (aEvent){
    document.getElementById("browsers").addEventListener("DOMContentLoaded", function onWindowLoad(aEvent){
        window.messageManager.loadFrameScript("chrome://myExtension/content/jquery.js", true);
        window.messageManager.loadFrameScript("chrome://myExtension/content/content.js", true);
}, false);

jquery.js

addEventListener('DOMContentLoaded', function(event) {
    with(content){
        /* jQuery core code goes here */
    }
}, true);

content.js

addEventListener('DOMContentLoaded', function(aEvent) { // on page load
    with(content) {
         if (aEvent.originalTarget.location.href != null) {
             if (aEvent.originalTarget.location.href == document.location.href && document.location.href != 'about:home') {
                //alert(jQuery(document).attr('title') + '\n' + jQuery(location).attr('href'));
             }
         }
    }
}, true);
benkol
  • 351
  • 1
  • 13
  • I Have some issue with this. My content script code only run if I keep open the "about:memory"/"about:addons"/"about:compartments" tab open. Otherwise contentscript doesn't seem to work. Any help would be appricieated. – MKumar May 31 '12 at 10:36