简体   繁体   English

Chrome浏览器在窗口聚焦时读取

[英]Chrome to read when window is focused,aka opened

I am still fairly new at programing. 我在编程方面还很陌生。 I have played around with JavaScript before, but it is still tricky for me. 我以前玩过JavaScript,但是对我来说仍然很棘手。

I got this great idea for an extension for Google Chrome- in the future it would be nice to port it into other browsers. 我对Google Chrome浏览器扩展有个好主意-将来最好将其移植到其他浏览器中。 For now I think Google Chrome would be the easiest way to develop for. 目前,我认为Google Chrome将是最简单的开发方法。

I investigated a little and finished the kitties tutorial on the extensions site . 我研究了一下,并在扩展站点上完成了kitty教程

From there it makes sense- easy for the most part, but my idea sounds impossible to me. 在大多数情况下,从那里讲起来很容易,但是我的想法对我来说听起来是不可能的。 Simply, the extension would automatically reload the browser when window is selected or focused on the screen. 简而言之,当选择窗口或将其聚焦在屏幕上时,扩展程序将自动重新加载浏览器。 Saves time by not pressing Ctrl + R (PC), or Cmd + R (Mac), or reload button every time the developer checks an update on code. 每次开发人员检查代码更新时,通过不按Ctrl + R (PC)或Cmd + R (Mac)或重新加载按钮来节省时间。

I was reading through the API documentation and found the method chrome.browserAction.onClick , is there like chrome.browserAction.focused ? 我正在阅读API文档,发现方法chrome.browserAction.onClick ,是否像chrome.browserAction.focused一样? Is this idea even feasible? 这个想法是否可行?

I also have to take in consideration that Chrome is visited by multiple OS. 我还必须考虑到Chrome被多个操作系统访问。 I wonder if Mac OS, Linux, and Windows version need different JavaScript instruction to pull this off? 我想知道Mac OS,Linux和Windows版本是否需要不同的JavaScript指令才能实现? This simple idea is overwhelming... 这个简单的主意令人难以置信...

Thanks for input in advance :) 感谢您的提前输入:)

Sounds like you'll want to use chrome.windows.onFocusChanged : 听起来您会想要使用chrome.windows.onFocusChanged

Fired when the currently focused window changes. 当前焦点窗口更改时触发。 Will be chrome.windows.WINDOW_ID_NONE if all chrome windows have lost focus. 如果所有Chrome窗口都失去焦点,则将为chrome.windows.WINDOW_ID_NONE。 Note: On some Linux window managers, WINDOW_ID_NONE will always be sent immediately preceding a switch from one chrome window to another. 注意:在某些Linux窗口管理器中,总是在从一个Chrome窗口切换到另一个Chrome窗口之前立即发送WINDOW_ID_NONE。

Here's an example of how to reload a newly-focused window's active tab: 这是有关如何重新加载新聚焦的窗口的活动选项卡的示例:

chrome.windows.onFocusChanged.addListener(function(windowId) {
    if (windowId != chrome.windows.WINDOW_ID_NONE) {
        chrome.tabs.query({ active:true, windowId:windowId }, function(tabs) {
            if (tabs.length == 1) {
                var tab = tabs[0];
                chrome.tabs.reload(tab.id);
            }
        });
    }
});

You'll also need to declare the tabs permission in your manifest. 您还需要在清单中声明tabs权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM