Firefox附加预取链接标记 [英] Firefox add-on prefetching with link tag
问题描述
main.js:
$ $ p $
exports.main =
var commentFinder = pageMod.PageMod({
include:*,
contentScriptFile:data.url(prefetch.js),
attachTo: (worker){
worker.port.emit(init);
}
});
onAttach:
}
prefetch.js:
var start ='< link rel =prefetchhref =';
var end ='>'
var links = [] ;
var aTags = document.body.getElementsByTagName(a)
(var i = 0; i< aTags.length; i ++){
var href = aTags [ I] .getAttribute(的 href);
if(links.indexOf(href)== -1)
links.push(href);
$ b $ for(var i = 0; i< links.length; i ++){
console.log(links [i]);
var tag = start + String(links [i])+ end;
document.head.innerHTML = tag + document.head.innerHTML;
}
当我在要求的页面的HTML中包含链接标签时,获取预取,但是当我使用附加组件添加链接标记时,它们不会。我认为这个问题可能是Firefox检查链接标记时,但我不确定。有没有什么办法来测试/修复这个问题?
解决方案我查看了代码,看起来
< link rel =prefetch>
元素仅在解析器仍处于活动状态时处理(请参阅 ( hg ))。因此,它在网站中使用内联脚本,但不使用常规的page-mod
。
您可以尝试更早地附加脚本,即将
contentScriptWhen:start
添加到您的页面模式选项中。但是你应该知道,在这一点上,只有文档元素存在于DOM,但没有别的,所以你需要让你的脚本,然后等待其他节点... ... $ /
$ b $另一种方法是,不是创建< link>
元素,而是使用 main.js ,然后通过require(chrome)
调用服务。 / p>
另外:从带宽和性能角度无条件地预取所有链接可能不是最好的想法。
另外:不是所有的链接都可以预取(
mailto:
等)。I'm trying to create a simple Firefox add-on to prefetch some webpages. I'd like to use Firefox's link tag to do the prefetching, since it seems like that's the easiest way. Here's my code:
main.js:
exports.main = function() { var commentFinder = pageMod.PageMod({ include: "*", contentScriptFile: data.url("prefetch.js"), attachTo: ["top"], onAttach: function(worker) { worker.port.emit("init"); } }); }
prefetch.js:
var start = '<link rel="prefetch" href="'; var end = '">' var links = []; var aTags = document.body.getElementsByTagName("a") for(var i=0; i<aTags.length; i++){ var href = aTags[i].getAttribute("href"); if(links.indexOf(href) == -1) links.push(href); } for(var i=0;i<links.length;i++){ console.log(links[i]); var tag = start + String(links[i]) + end; document.head.innerHTML = tag + document.head.innerHTML; }
When I include the link tags in the HTML of the pages I request, they get prefetched, but when I add in link tags using the add-on, they don't. I think the problem might be when Firefox is checking for the link tag, but I'm not sure. Is there any way to test/fix this?
解决方案I looked through the code, and it appears that
<link rel="prefetch">
elements are only processed when the parser is still active (see the call graph ofProcessLink
(hg)). Hence it works when using an inline script in a web site, but not using a regularpage-mod
.You may try to attach the script earlier, i.e add
contentScriptWhen: "start"
to your page-mod options. But you should be aware that at this point there is only the document element present in the DOM, but nothing else, so you need to have your script then wait for other nodes...Another alternative is that instead of creating
<link>
elements, you could use thensIPrefetchService
, by having your content-script message-pass a list of links/referrers to yourmain.js
, which would then call the service viarequire("chrome")
.Aside: It might not be the best idea to unconditionally prefetch all links from a bandwidth and performance perspective.
Also aside: Not all links are prefetch-able (
mailto:
etc).这篇关于Firefox附加预取链接标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!