Results 1 to 6 of 6

Thread: Need to use jQuery in content process of Mozilla Firefox

  1. #1
    Join Date
    Nov 2011
    Posts
    60

    Need to use jQuery in content process of Mozilla Firefox

    I am getting an issue while developing fennec extension. Is there any one who has managed to use jQuery on content process? I tried to load by using loadScriptFrame at overlay process but it not able to extend either window or content global variable. Let me know if you are having any solution for the same. thanks a lot in advance.

  2. #2
    Join Date
    May 2011
    Posts
    483

    Re: Need to use jQuery in content process of Mozilla Firefox

    Let me know whether you are using any add-on SDK. If you are having content script then you can load jquery by using following addon script.
    Code:
    var data = require("self").data;
     
    var reddit_panel = require("panel").Panel({
      width: 240,
      height: 320,
      contentURL: "http://www.reddit.com/.mobile?keep_extension=True",
      contentScriptFile: [data.url("jquery-1.4.4.min.js"),
                          data.url("panel.js")]
    });
     
    reddit_panel.port.on("click", function(url) {
      require("tabs").open(url);
    });
     
    require("widget").Widget({
      id: "open-reddit-btn",
      label: "Reddit",
      contentURL: "http://www.reddit.com/static/favicon.ico",
      panel: reddit_panel
    });
    Following is panel.js content script which would intercepts with link clicks
    Code:
    $(window).click(function (event) {
      var t = event.target;
     
      // Don't intercept the click if it isn't on a link.
      if (t.nodeName != "A")
        return;
     
      // Don't intercept the click if it was on one of the links in the header
      // or next/previous footer, since those links should load in the panel itself.
      if ($(t).parents('#header').length || $(t).parents('.nextprev').length)
        return;
     
      // Intercept the click, passing it to the addon, which will load it in a tab.
      event.stopPropagation();
      event.preventDefault();
      self.port.emit('click', t.toString());
    });

  3. #3
    Join Date
    May 2011
    Posts
    378

    Re: Need to use jQuery in content process of Mozilla Firefox

    Well Content script requires access to DOM objects in the arbitrary web pages, however this could cause potential security problem. Any malicious page can be redefined as standard functions and properties of DOM objects. In order to deal with content scripts should access the DOM objects through proxy.
    The proxy would be based on XRayWrapper. This particular wrapper would give the access to the native values of DOM functions as well as properties even if the scripts has been redefined. Proxy would be transparent to content scripts. content script will access DOM directly.

  4. #4
    Join Date
    May 2011
    Posts
    450

    Re: Need to use jQuery in content process of Mozilla Firefox

    In case you have loaded number of content scripts the same document can interact with the each other and web content as well. This particular thing will make you to load jQuery and then you have to load your scripts which is making use of jQuery. Content scripts which has been loaded into the different document will not be able to interact with each other.

  5. #5
    Join Date
    Jun 2011
    Posts
    501

    Re: Need to use jQuery in content process of Mozilla Firefox

    You will be able to communicate between content script and page scripts by making use of postmessge(). In the previous version of the SDK global postMessage() function in the content scripts used for communicating between content script and add-on code. You will not be able to make use of usewindow.postMessage(). Rather than you will need to use document.defaultView.postMessage()
    Below mentioned is window.addEventListener to listen the message.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang='en' xml:lang='en' xmlns="http://www.w3.org/1999/xhtml">
     
    <head>
        <script>
          window.addEventListener("message", function(event) {
            window.alert(event.data);
          }, false);
        </script>
    </head>
    </html>
    Content script can send the message by using
    Code:
    document.defaultView.postMessage()
    
    var widgets = require("widget");
    var tabs = require("tabs");
    var data = require("self").data;
     
    var widget = widgets.Widget({
      id: "postMessage",
      label: "demonstrate document.defaultView.postMessage",
      contentURL: "http://www.mozilla.org/favicon.ico",
      onClick: function() {
        tabs.activeTab.attach({
          contentScript: "document.defaultView.postMessage('hi there!', '*');"
        });
      }
    });
     
    tabs.open(data.url("listener.html"));

  6. #6
    Join Date
    Jul 2011
    Posts
    397

    Re: Need to use jQuery in content process of Mozilla Firefox

    if you wanted to access e underlying DOM then you can use global unsafeWindow object. But personally I don’t recommend to make use of unsafeWindow it is same as that of Greasemonkey’s unsafeWindow. Also it is not having support for any API and it could be about to removed or change the future version of SDK.

Similar Threads

  1. Replies: 8
    Last Post: 04-12-2011, 11:21 AM
  2. Corrupted Content error in Mozilla Firefox
    By Tikoo in forum Technology & Internet
    Replies: 8
    Last Post: 06-11-2011, 11:02 PM
  3. YouTube not working on Mozilla Firefox 6 and Mozilla Firefox 7
    By Kedar!nath in forum Technology & Internet
    Replies: 7
    Last Post: 29-08-2011, 01:32 AM
  4. Difficulties in Mozilla Firefox 5.0 and Mozilla Firefox 6.0 ?
    By Udyami in forum Technology & Internet
    Replies: 3
    Last Post: 27-07-2011, 09:11 AM
  5. Loading content of a text .txt file using jQuery
    By Gap meer in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 07:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,880,423.84762 seconds with 17 queries