首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用windows.WindowState API (JavaScript)?

如何使用windows.WindowState API (JavaScript)?
EN

Stack Overflow用户
提问于 2021-01-30 17:34:08
回答 2查看 639关注 0票数 0

我喜欢使用windows.WindowState API,但得到的错误是“未定义窗口”。

代码语言:javascript
复制
const windowState = windows.WindowState;

我正在开发一个WebExtension,它使用了语音合成API。问题是,当语音暂停时,API就不能再在其他选项卡上使用了!为了防止这种情况,必须停止讲话,而不是停顿。但在这种情况下,如果用户希望在第一个选项卡上再次恢复文本,则整个段落必须从头开始重复。这就是为什么我想区分不同的粘着性变化(我使用的是“视觉变化”、“模糊”和“焦点”事件):

  • TabLeave➔停止语音
  • 如果用户设置了此设置,则TabEnter➔重新启动语音
  • 如果用户设置了此设置,则LostFocus & WndMinimized➔暂停语音
  • GotFocus &WndRestored/Wndximized➔简历语音,如果它在此之前被暂停并且用户进行了此设置

我的"manifest.json":

代码语言:javascript
复制
{
    "description": "My extension.",
    "manifest_version": 2,
    "name": "Lesehilfe",
    "version": "1.0",
    "homepage_url": "https://dummy.de",

    "icons": {
        "32": "icons/aero_arrow_reading_aid_32x32.cur"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["lib/tinycolor.js", "lib/input_action.js", "lib/state_change.js", "lib/print.js", "lib/xLabs_library.js", "options.js", "content.js"],
            "css": ["style.css", "resources.css"]
        }
    ],

    "background": {
        "scripts": ["background.js"]
    },

    "options_ui": {
        "page": "options.html",
        "browser_style": true,
        "chrome_style": true,
        "_comment": "browser_style is for Firefox, non-standard chrome_style for Chrome and Opera"
    },

    "permissions": [
        "storage",
        "notifications",
        "activeTab"
    ]
}

问题演示:

代码语言:javascript
复制
<html>
    <body style="text-align: center;">
        <strong>Speech Synthesis API Test</strong>
        <p id="SOquestion">
I am developing a WebExtension, which makes use of the speech synthesis API. The problem with it is, when speech is paused, that API can’t be used on other tabs anymore! To prevent this, that speech has to be stopped instead of paused. But in this case, the whole paragraph has to be repeated from start, if the user wants to resume the text on the first tab again. That is, why I want to distinguish between different visability changes (I am using the 'visibilitychange', 'blur' and 'focus' event for this):
        </p>
        <button id="btnPlayPause">▶</button><!-- Play sign -->
        <button id="btnStop">⏹</button>    <!-- Stop sign -->
        <script>
            function onCompletion(e) {
                btnPlayPause.innerText = "▶";  // Play sign
            }

            var btnPlayPause = document.getElementById("btnPlayPause");
            var btnStop = document.getElementById("btnStop");
            var text = document.getElementById("SOquestion").innerText;

            var synth = window.speechSynthesis;
            var utterThis = new SpeechSynthesisUtterance(text);
            utterThis.onend = onCompletion;
            utterThis.lang = 'en-UK';

            btnPlayPause.addEventListener('click', (e) => {
                if (synth.paused) {
                    btnPlayPause.innerText = "⏸";  // Pause sign
                    synth.resume();
                    return;
                }
                if (synth.speaking) {
                    btnPlayPause.innerText = "▶";   // Play sign
                    synth.pause();
                    return;
                }
                btnPlayPause.innerText = "⏸";      // Pause sign
                synth.speak(utterThis);
            }, false);
            
            btnStop.addEventListener('click', (e) => {
                onCompletion();
                synth.cancel();
            }, false);
        </script>
    </body>
</html>

请遵循以下步骤:

  • 运行代码段
  • 点击播放按钮
  • 单击“暂停”(相同的按钮)
  • 导航到其他选项卡,其中有一篇文章
  • 进入Firefox读取器模式
  • 开始语音合成

➔它不应该播放任何语音输出(至少发生在我身上)

  • 现在,将其与停止演讲而不是停顿进行比较。

我的问题是:

  • windows.WindowState API仅限于后台脚本,还是也可以在内容脚本上使用?
  • 它需要什么许可?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-07 23:27:17

  1. 问题(内容/背景):

windows.WindowState API不包含在这份清单中,因此只能在后台脚本中使用。基本对象是browserchrome。下面是用于onMessage侦听器的几行代码:

代码语言:javascript
复制
// does only work on Firefox (or Chromium with polyfill) due to the use of promise
return browser.windows.getCurrent().then(win => {
    return {windowState: win.state};
});

// old version for Chromium
chrome.windows.getCurrent(null, (win) => {
    sendResponse({windowState: win.state});
});
return true;
  1. 问题(准许):

此API不需要任何权限。

票数 0
EN

Stack Overflow用户

发布于 2021-01-30 17:45:20

windows API只能在扩展中使用,如果使用的是扩展名,则需要在扩展名的manifest.json文件中请求权限。

然后你就像这样使用它:

代码语言:javascript
复制
const windowState = windows.WindowState;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65971115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档