1

我在访问 Chrome 的标签 ID 时遇到问题。我可以获取它,但它仍保留扩展程序中,并且我无法在扩展程序之外使用它,尽管我能够在扩展程序之外记录键盘事件。

这是我正在尝试做的事情:

  1. 用户导航到选项卡并使用“捕获”按钮获取 tabId
  2. tabId 存储为全局变量
  3. 然后,用户可以导航到其浏览器中的任何其他选项卡,并从那里使用组合键,用户可以通过同时按 CTRL + SHIFT 在任何给定时刻重新加载捕获的选项卡

扩展名.html

<!DOCTYPE html>
<html>
<head>
  <title>Extension</title>
  <style>
  body {
    min-width: 357px;
    overflow-x: hidden;
  }
  </style>
    <p>Step 1. Navigate to tab you want to refresh and click the 'capture' button</p>
    <button type="button" id="capture">Capture!</button>
    <p id="page"></p>
    <p>Step 2. Now you can reload that tab from anywhere by pressing CTRL+SHIFT simultaneously</p>
  </div>

  <script src="contentscript.js"></script>
</head>
<body>
</body>
</html>

清单.json

{
  "manifest_version": 2,

  "name": "Extension",
  "description": "This extension allows you to trigger page refresh on key combinations from anywhere",
  "version": "1.0",

  "content_scripts": [
    {
      "matches": ["http://*/*","https://*/*"],
      "run_at": "document_end",
      "js": ["contentscript.js"]
    }
  ],

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "extension.html"
  },
   "web_accessible_resources": ["script.js"],
   "permissions": [
    "tabs"
  ],
}

内容脚本.js

var s = document.createElement('script');
s.src = chrome.extension.getURL("script.js");
(document.head||document.documentElement).appendChild(s);
s.parentNode.removeChild(s);

脚本.js

'use strict';

var isCtrl = false;
var tabId = 0;

document.onkeyup=function(e){
    if(e.which === 17) {
        isCtrl=false;
    }
};

document.onkeydown=function(e){
    if(e.which === 17) {
        isCtrl=true;
    }
    if(e.which === 16 && isCtrl === true) {
        /* the code below will execute when CTRL + SHIFT are pressed */

        /* end of code */
        return false;
    }
};

document.getElementById('capture').onclick = function(){
    chrome.tabs.getSelected(null, function(tab) {
        tabId = tab.id;
        document.getElementById('page').innerText = tab.id;
    });
};

我认为这将是解决方案,但它没有奏效:

/* the code below will execute when CTRL + SHIFT are pressed */

chrome.tabs.getSelected(null, function(tab) {
   chrome.tabs.reload(tabId);
});

/* end of code */

作为var tabId = 0;全局变量似乎毫无意义,所以我认为消息传递应该是解决方案,但问题是我不明白我应该如何实现它。

关于如何根据其 ID 从任何地方刷新选项卡的任何建议?

4

1 回答 1

1

contentscript.js只是一个带有用 JavaScript 编写的程序指令的文件。每次将这些指令加载到特定的执行环境中时,它们都会被解释为新的和新的。您的弹出窗口和内容脚本是独立的执行环境。

contentscript.js文件本身不存储状态。当contentscript.js在内容脚本环境中加载时,内容脚本执行环境不知道在哪里contentscript.js包含了其他内容。

此处使用的正确模式是让后台页面保持状态并记住最后捕获的选项卡的选项卡 ID。弹出窗口将使用消息传递将当前选项卡 ID 发送到后台页面(chrome.runtime.sendMessage在弹出窗口和chrome.runtime.onMessage后台页面中使用)。然后,稍后,内容脚本会在看到新闻时向后台页面发送消息Ctrl+Shift,并且后台页面会调用chrome.tabs.reload(tabId).

extension.html里面,而不是你当前的<script>标签:

document.getElementById("capture").onclick = function() {
    chrome.tabs.getSelected(null, function(tab) {
        tabId = tab.id;

        // send a request to the background page to store a new tabId
        chrome.runtime.sendMessage({type:"new tabid", tabid:tabId});
    });
};

contentscript.js里面:

/* the code below will execute when CTRL + SHIFT are pressed */

// signal to the background page that it's time to refresh
chrome.runtime.sendMessage({type:"refresh"});

/* end of code */

背景.js

// maintaining state in the background
var tabId = null;

// listening for new tabIds and refresh requests
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {

        // if this is a store request, save the tabid
        if(request.type == "new tabid") {
            tabId = request.tabid;
        }

        // if this is a refresh request, refresh the tab if it has been set
        else if(request.type == "refresh" && tabId !== null) {
            chrome.tabs.reload(tabId);
        }
});
于 2013-07-09T19:52:41.747 回答