【问题标题】:Write a chrome extension to console.log the active tab URL whenever a new page is loaded每当加载新页面时,将 chrome 扩展程序写入 console.log 活动选项卡 URL
【发布时间】:2019-07-15 18:45:08
【问题描述】:

我想编写一个 chrome 扩展程序,它记录每次加载新站点时当前活动的标签 URL 并将其发送到服务器以供进一步使用。到目前为止,我已经设法编写了以下代码: ma​​nifest.json

{
    "manifest_version": 2,
    "name": "Currenturl",
    "description": "Fetches current tab url.",
    "version": "0.1",
    "author": "Tarun Khare",
    "browser_action": {
        "default_icon": "icon.png",
        "default_title": "Just observing your current url."
    },
    "permissions": ["tabs", "activeTab"],
    "background": {
        "scripts": ["content.js"],
        "persistent": false
    }
}

content.js

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
    console.log("hello: "+url);
});

我正在使用后台脚本,因为 chrome.tabs 在内容脚本中不起作用。但是这个扩展并没有在 chrome 控制台中打印任何东西。有什么问题?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

    您可以在 background.js 中计算和提取包含完整详细信息的 URL 这是 GihHub 存储库下面的主要代码:

        chrome.windows.getAll({ populate: true }, function (windows) {
        windows.forEach(function (window) {
        window.tabs.forEach(function (tab) {
        //i++
        collect all of the urls here, I will just log them instead
        console.log("tab.ur[![enter image description here][1]][1]l aaaaakahari 2");
            console.log(tab.url);
        });
      });
    });
    

    这里有一个chrome Extension的GitHub存储库,您可以在background.js中找到计算和提取打开URL的方法,甚至还有即将控制台的部分当用户打开或关闭任何选项卡时。​​p>

    https://github.com/Farbod29/extract-and-find-the-new-tab-frome-the-browser-with-chrome-extention

    【讨论】:

      【解决方案2】:

      我尝试根据您提供的信息编写代码。

      const tabUpdatelistenerFun = (tabid, changeInfo, tab) => {
        const url = changeInfo.url;
      
        if (!url || ['chrome://', 'about://'].some(p => url.startsWith(p))) return false;
      
        const { index, active, highlighted, windowId } = tab;
      
        if (!active) return false;
      
        chrome.tabs.query({ index, highlighted, windowId, lastFocusedWindow: true }, () => {
          console.log(url);
        })
      }
      
      chrome.tabs.onUpdated.addListener(tabUpdatelistenerFun);
      

      我想这就是你想要的。

      【讨论】:

        【解决方案3】:
        1. 将 content.js 重命名为 background.js,因为这是一个后台脚本
        2. 使用chrome.tabs.onUpdated监听器
        3. 查看正确的控制台:Where to read console messages from background.js?

        chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
          if (change.url) {
            console.log(change.url);
          }
        });
        

        它将报告所有标签中的 URL 更改。
        您还可以通过添加对 tab.active 属性的检查来将处理限制为仅活动选项卡。

        【讨论】:

        • 你能告诉代码获取当前焦点标签的网址吗?
        • 这是您问题中的代码,但它不是监听器。
        猜你喜欢
        • 2013-09-05
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多