我正在为 Firefox 编写扩展程序。使用 dom.location 跟踪访问过的搜索结果页面,我得到了这个网址http://www.google.com/search?hl=en&source=hp&q=hi&aq=f&aqi=&oq=&fp=642c18fb4411ca2e。如果你点击它,“hi”的谷歌搜索结果应该会出现。您会从标题栏中知道这一点 - 因为页面的其余部分不会加载。任何谷歌搜索都会发生这种情况。奇怪的是,如果你切断它的一部分,比如说,http://www.google.com/search?hl= en&source=hp&q=hi - 它有效!但是我自己在谷歌上搜索“嗨”确实给了我一个较长的 URL - http://www.google.com/#hl=en&source=hp&q=hi&aq=f&aqi=&oq=&fp=db658cc5049dc510。
任何人都可以从中找到理由吗?
我刚刚再次尝试了我的实验,这次将原始 URL 保存在地址栏中。事实证明,dom.location.href 给出了不同的值。这是怎么回事?
原文:
http ://www.google.com/#hl=en&source=hp&q=hi&aq=f&aqi=&oq=&fp=642c18fb4411ca2e
dom.location.href
http://www.google.com/search?hl=en&source=hp&q=hi&aq=f&aqi=&oq=&fp=642c18fb4411ca2e
window.addEventListener("load", function() { myExtension.init(); }, false);
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
var messagepane = document.getElementById("messagepane"); // mail
if(messagepane)
messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
},
onPageLoad: function(aEvent) {
var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
// do something with the loaded page.
// doc.location is a Location object (see below for a link).
// You can use it to make your code executed on certain pages only.
var url = doc.location.href;
if (url.match(/(?:p|q)(?:=)([^%]*)/)) {alert("MATCH" + url);resultsPages.push(url);} else {alert(url);
}
}
该片段直接来自Mozilla,并带有我自己的匹配和警报。我很抱歉没有早点发布代码。