我已经写了一些代码工作正常,除非因未知原因一个函数不起作用(函数不调用,如果不存在)。函数调用不工作在Firefox代码的Firefox扩展使用谷歌框
我的代码的目的是检测我是否使用Google搜索引擎。它的工作方式如下:当用户在Google搜索字段中输入关键字并输入类型或点击“搜索”时,url会发生变化,我的代码会检测到它,并显示一个带有页面URL的Javascript警报,另一个显示文本“它匹配”。
但问题是,当我直接在Google框中键入关键字时,它不再起作用。
我的代码:
function displayUrl(url)
{
if(url[0].match(/^http:\/\/www\.google\.fr/))
{
alert('It matches');
}
}
function getUrlVars(href)
{
var vars = [], hash;
var hashes = href.slice(href.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);
“不工作”***如何***?你期望什么,而它在做什么?你在控制台看到什么错误? –
@TJ Crowder我的意思是函数调用不工作,没有任何东西出现(就好像函数调用不存在一样)。我目前正在探索错误控制台。 – Bruno