我喜欢使用windows.WindowState API,但得到的错误是“未定义窗口”。
const windowState = windows.WindowState;
我正在开发一个WebExtension,它使用了语音合成API。问题是,当语音暂停时,API就不能再在其他选项卡上使用了!为了防止这种情况,必须停止讲话,而不是停顿。但在这种情况下,如果用户希望在第一个选项卡上再次恢复文本,则整个段落必须从头开始重复。这就是为什么我想区分不同的粘着性变化(我使用的是“视觉变化”、“模糊”和“焦点”事件):
我的"manifest.json":
{
"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"
]
}问题演示:
<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>
请遵循以下步骤:
➔它不应该播放任何语音输出(至少发生在我身上)
我的问题是:
发布于 2021-02-07 23:27:17
windows.WindowState API不包含在这份清单中,因此只能在后台脚本中使用。基本对象是browser或chrome。下面是用于onMessage侦听器的几行代码:
// 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;此API不需要任何权限。
发布于 2021-01-30 17:45:20
windows API只能在扩展中使用,如果使用的是扩展名,则需要在扩展名的manifest.json文件中请求权限。
然后你就像这样使用它:
const windowState = windows.WindowState;https://stackoverflow.com/questions/65971115
复制相似问题