Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将chrome扩展写入console。每当加载新页面时,记录活动选项卡URL_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 将chrome扩展写入console。每当加载新页面时,记录活动选项卡URL

Javascript 将chrome扩展写入console。每当加载新页面时,记录活动选项卡URL,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我想写一个chrome扩展,在每次加载新站点时记录当前的活动选项卡URL,并将其发送到服务器以供进一步使用。到目前为止,我已经成功地编写了以下代码: manifest.json { "manifest_version": 2, "name": "Currenturl", "description": "Fetches current tab url.", "version": "0.1", "author": "Tarun Khare", "bro

我想写一个chrome扩展,在每次加载新站点时记录当前的活动选项卡URL,并将其发送到服务器以供进一步使用。到目前为止,我已经成功地编写了以下代码: manifest.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控制台中打印任何内容。问题是什么

  • 将content.js重命名为background.js,因为这是一个后台脚本
  • 使用侦听器
  • 查看正确的控制台:

  • 它将报告所有选项卡中的URL更改。
    还可以通过添加属性检查,将处理限制为仅活动选项卡

  • 将content.js重命名为background.js,因为这是一个后台脚本
  • 使用侦听器
  • 查看正确的控制台:

  • 它将报告所有选项卡中的URL更改。

    您还可以通过添加属性检查将处理限制为仅活动选项卡。

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

    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);
    

    我想这就是您想要的。

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

    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);
    

    我想这就是你想要的。

    你能告诉代码获取当前焦点选项卡的url吗?这是你问题中的代码,但不是侦听器。你能告诉代码获取当前焦点选项卡的url吗?这是你问题中的代码,但不是侦听器。