简体   繁体   English

有没有一种更快的方法来在网页的URL /链接旁边添加图标-替代使用“ onload”事件?

[英]Is there a faster way to add icons next to urls/links in a web page - alternative to using “onload” event?

I am writing a Firefox add-on that adds little icons next to all the urls in a web page. 我正在写一个Firefox附加组件,它在网页中的所有URL旁边添加了一些小图标。

The way I am doing it right now is : 我现在做的方式是:

  1. window.addEventListener("load", AddIconsToURLs, false); window.addEventListener(“ load”,AddIconsToURLs,false);
  2. In AddIconsToURLs function: Get all elements by tag name "a" For each "a" element, append an "img" element. 在AddIconsToURLs函数中:通过标记名称“ a”获取所有元素对于每个“ a”元素,请附加一个“ img”元素。

However, for some web pages, the icons take a lot of time to appear since the "onload" event takes time. 但是,对于某些网页,由于“ onload”事件需要花费时间,因此图标需要花费很多时间才能显示。

Is there any faster way to make this happen ? 有没有更快的方法可以做到这一点? Rather than waiting for all the links to load and then adding icons next to each of them, is there any way to add an icon next to a link as soon as that particular link is loaded ? 除了等待所有链接加载然后在每个链接旁边添加图标外,是否有任何方法在加载特定链接后立即在链接旁边添加图标?

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

What do you want to do with the images? 您要如何处理图像? Anything special? 有什么特别的吗? If it is just for the look, much easier would be to load a CSS style sheet in your addon and use the :after pseudo element: 如果仅是外观,将更容易在您的插件中加载CSS样式表并使用:after伪元素:

a:after {
  content: url('chrome://youraddon/skin/imagefile.png');
}

No JavaScript processing involved :) 不涉及JavaScript处理:)

You can load a custom CSS file with: 您可以使用以下方式加载自定义CSS文件:

var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
          .getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
          .getService(Components.interfaces.nsIIOService);
var uri = ios.newURI("chrome://youraddon/skin/cssfile.css", null, null);

if(!sss.sheetRegistered(uri, sss.AGENT_SHEET)) {
    sss.loadAndRegisterSheet(uri, sss.AGENT_SHEET);
}

See also Using the Stylesheet Service . 另请参阅使用样式表服务

您可以听另一个事件:

document.addEventListener("DOMContentLoaded", AddIconsToURLs, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM