代码之家  ›  专栏  ›  技术社区  ›  int3

Chrome浏览器扩展:如何激活特定页面的所有传出链接的页面操作?

  •  1
  • int3  · 技术社区  · 15 年前

    我希望为某个页面的所有传出链接激活页面操作。我该怎么做呢?我已经把文件看了一遍,没有用。任何提示都会受到赞赏!

    2 回复  |  直到 15 年前
        1
  •  1
  •   uthark    15 年前
    1. Google Chrome API没有这样的API,但是您想要的功能可以使用标准的Google Chrome扩展API来实现。
    2. 你需要实施 content script
    3. 你的内容脚本应该修改你想要处理的页面的DOM,并用你的自定义javascript覆盖所有的外发链接,它将做一些事情并打开点击链接。

    要修改link href,可以执行以下操作:

    function processLink(element, newHref) {
      var a = document.createElement("a");
      a.href        = newHref;
      a.textContent = element.textContent;
      a.title       = element.title;
    
      element.parentNode.replaceChild(a, element);
    }
    

    更新1。

    您可以生成类似

    a.href = "javascript:processOutgoingLinkClick('" + element.href + "')"

    功能 processOutgoingLinkClick 应该包含点击的实际处理。

        2
  •  0
  •   Mohamed Mansour    15 年前

    只是好奇,你为什么不用 Chrome Extensions Tab Events 您可以收听OnUpdated OnCreated。当用户单击页面上的链接时,它将在OnUpdated中启动一个事件。

    因此,在background.html中,您可以执行以下操作:

    chrome.tabs.onUpdated.addListener(function(tabId, info) {
      if (info.status === 'loading')
        console.log('Loading url ... ' + info.url)
    });
    

    同样的事情。然后在加载时,您可以决定如何处理pageAction。