开发者

Function call not working in Javascript code for Firefox extension when using the Google box

I've written some code which works fine except that for an unknown reason a function does not work (the function is not called as if not existing).

The aim of my code is to detect if I'm using Google search engine. It works this way : when a user puts a keyword on the Google search field and types enter or clicks on "Search" the url changes and my code detects it and displays a Javascript alert with the URL of the page and another with the text "It matches".

But the problem is that when I type the keyword directly in the Google box it doesn't work anymore.

My code:

    function displayUrl(url)
    { 
       if(url[0].match(/^http:\/\/www\.google\.fr/))
       {
           alert('It matches');
       }
    }

    function getUrlVars(href)
    {
       var vars = [], hash;
       var hashes = href.slice(hre开发者_开发问答f.indexOf('?') + 1).split('&');
       for(var i = 0; i < hashes.length; i++)
       {
           hash = hashes[i].split('=');
           vars.push(hash[0]);
           vars[hash[0]] = hash[1];
       }
       displayUrl(vars);
    }

    var myExt_urlBarListener = {
     QueryInterface: function(aIID)
     {
       if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
           aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
           aIID.equals(Components.interfaces.nsISupports))
         return this;
       throw Components.results.NS_NOINTERFACE;
    },

    onLocationChange: function(aProgress, aRequest, aURI)
    {
       myExtension.processNewURL(aURI);
    },

    onStateChange: function(a, b, c, d) {},
    onProgressChange: function(a, b, c, d, e, f) {},
    onStatusChange: function(a, b, c, d) {},
    onSecurityChange: function(a, b, c) {}
    };

    var myExtension = {
     oldURL: null,

     init: function() {
        // Listen for webpage loads
        gBrowser.addProgressListener(myExt_urlBarListener,
            Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
     },

     uninit: function() {
       gBrowser.removeProgressListener(myExt_urlBarListener);
     },

     processNewURL: function(aURI) {
        if (aURI.spec == this.oldURL)
          return;

       // now we know the url is new...
       alert(aURI.spec);
       /* 
       the following function call doesn't work
       when I type the keyword in the Google box instead of using the Google
       traditionnal search field          
       */
       getUrlVars(aURI.spec);
       this.oldURL = aURI.spec;

     }
  };

  window.addEventListener("load", function() {myExtension.init()}, false);
  window.addEventListener("unload", function() {myExtension.uninit()}, false);


This should work on a adress like

http://domain.com/page.php?http://www.google.fr=something

Inside displayUrl you're checking if the key of the first GET-parameter of the url provided to getUrlVars() matches http://www.google.fr . I guess that's not what you want^^

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜