Firefox附加预取链接标记 [英] Firefox add-on prefetching with link tag

查看:144
本文介绍了Firefox附加预取链接标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的Firefox插件来预取一些网页。我想用Firefox的链接标签来进行预取,因为看起来这是最简单的方法。这是我的代码:

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 of ProcessLink(hg)). Hence it works when using an inline script in a web site, but not using a regular page-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 the nsIPrefetchService, by having your content-script message-pass a list of links/referrers to your main.js, which would then call the service via require("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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆