将焦点转移到Chrome扩展程序中的标签页

时间:2018-01-29 20:04:32

标签: google-chrome google-chrome-extension manifest.json

在我的扩展程序中,我创建了一个小弹出窗口:

style.cursor = 'crosshair'

然后将一些JavaScript注入活动选项卡中加载的页面。 它应该改变光标形状并注册一个' onclick'事件

它工作正常,但是,用户需要单击页面/选项卡两次。 第一次单击将焦点转移到页面/选项卡,只有第二次单击由onclick处理程序处理。 同样,光标形状(chrome.tabs.update({"active":true});)仅在第一次单击后才会更改。

如何将焦点自动转移到标签? 我尝试了{ "manifest_version": 2, "name": "Stack Overflow", "version": "1.0", "browser_action": { "default_popup": "popup.html" }, "permissions": [ "tabs", "activeTab" ] } ,但这不起作用。

的manifest.json

<!doctype html>
<!--
 This page is shown when the extension button is clicked, because the
 "browser_action" field in manifest.json contains the "default_popup" key with
 value "popup.html".
 -->
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Question</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <div>How to transfer focus to the tab?</div>
  </body>
</html>

popup.html

document.addEventListener('DOMContentLoaded', function () {
    chrome.tabs.executeScript({ "file": "page.js" });
    chrome.tabs.update({"active":true});
    return;
});

popup.js

// The cursor is not updated until I click the tab.
document.body.style.cursor = 'crosshair';
document.addEventListener('click', function(){alert('you clicked me')});

page.js

options=keyValues

0 个答案:

没有答案