来自后台脚本的 window.state = 'fullscreen' 应该工作吗?
Is window.state = 'fullscreen' from a background script supposed to work?
在此处使用 Xubuntu 14.04 和 Firefox 45.0.1
如果 location.hash == "#fullscreen"
.
,我正在尝试从后台脚本中自动将浏览器 window 置于全屏状态
这是通过执行内容脚本侦听的 postMessage()
从特权网页脚本请求的,然后将此请求委托给后台脚本。
一切都按预期工作,包括 background.js
中的预期 console.log()
值(请参阅下面的相关源代码片段)...除了 window 不会全屏;什么都没有发生,也没有关于要求用户启动事件的控制台警告,如果我从网页本身尝试类似的东西,我 会 收到(这就是为什么我转而创建这个扩展,首先)。例如,尝试 w.state = 'minimized'
也无济于事。
问题:
Firefox WebExtensions API 是否应该支持 window.state
更改(已经)?
如果是这样,Firefox WebExtensions API 是否应该有足够的特权来启动全屏而无需明确的用户交互?
如果是这样,是否应该允许我从我正在尝试执行此操作的上下文中执行此操作?
(X)ubuntu 或任何 Firefox 首选项可能是罪魁祸首吗?
相关manifest.json
数据:
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://privilegeduri/*"],
"js": ["jquery-1.11.3.min.js", "content.js"],
"run_at": "document_start"
}
],
// I've tried without "fullscreen" as well
"permissions": [
"tabs",
"fullscreen", // no mention of this on MDN, but I tried it anyway
"webNavigation"
]
特权网页脚本:
if( location.hash == '#fullscreen' ) {
if( hasExtension() ) { // function that evaluates whether my extension is installed
window.postMessage( {
action: 'requestFullscreen'
}, 'http://privilegeduri' );
}
}
content.js
脚本:
function receiveMessage( e ) {
if( e.source === window && e.origin === 'http://privilegeduri' ) {
switch( e.data.action ) {
case 'requestFullscreen':
chrome.runtime.sendMessage( e.data );
break;
}
}
}
window.addEventListener( 'message', receiveMessage );
background.js
脚本:
function receiveMessage( message, sender, sendResponse ) {
if( sender.id === chrome.runtime.id && sender.url.match( /^http:\/\/privilegeduri/ ) ) {
switch( message.action ) {
case 'requestFullscreen':
browser.windows.get( sender.tab.windowId, {}, function( w ) {
console.log( w.state ); // outputs 'normal'
w.state = 'fullscreen';
console.log( w.state ); // outputs the expected value 'fullscreen'
} );
break;
}
}
}
chrome.runtime.onMessage.addListener( receiveMessage );
啊...我刚遇到 windows.update()
方法,当我尝试通过 updateInfo
参数对象设置 state
时,它 做了 通知:
Type error for parameter updateInfo (Property "state" is unsupported by Firefox) for windows.update
真可惜
现在是 2018 年,代码库发生了变化。如果您使用的是 Firefox Quantum,那么 windows.update() 现在可以按预期运行。这是一个简单的例子:
manifest.json:
{
"manifest_version": 2,
"name": "WootFullscreen",
"version": "9000.0.0.1",
"description": "Sends woot, gets fullscreen.",
"author": "Mekronid",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["messagesender.js"]
}
],
"background": {
"scripts": ["messagereciever.js"]
},
"permissions": ["tabs", "<all_urls>"]
}
messagesender.js:
browser.runtime.sendMessage({message: "woot"})
messagereciever.js:
browser.runtime.onMessage.addListener(listener)
async function listener(findWoot) {
console.log(findWoot.message)
var val = await browser.windows.getCurrent()
browser.windows.update(val.id, { state: "fullscreen"})
}
在此处使用 Xubuntu 14.04 和 Firefox 45.0.1
如果 location.hash == "#fullscreen"
.
这是通过执行内容脚本侦听的 postMessage()
从特权网页脚本请求的,然后将此请求委托给后台脚本。
一切都按预期工作,包括 background.js
中的预期 console.log()
值(请参阅下面的相关源代码片段)...除了 window 不会全屏;什么都没有发生,也没有关于要求用户启动事件的控制台警告,如果我从网页本身尝试类似的东西,我 会 收到(这就是为什么我转而创建这个扩展,首先)。例如,尝试 w.state = 'minimized'
也无济于事。
问题:
Firefox WebExtensions API 是否应该支持
window.state
更改(已经)?如果是这样,Firefox WebExtensions API 是否应该有足够的特权来启动全屏而无需明确的用户交互?
如果是这样,是否应该允许我从我正在尝试执行此操作的上下文中执行此操作?
(X)ubuntu 或任何 Firefox 首选项可能是罪魁祸首吗?
相关manifest.json
数据:
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://privilegeduri/*"],
"js": ["jquery-1.11.3.min.js", "content.js"],
"run_at": "document_start"
}
],
// I've tried without "fullscreen" as well
"permissions": [
"tabs",
"fullscreen", // no mention of this on MDN, but I tried it anyway
"webNavigation"
]
特权网页脚本:
if( location.hash == '#fullscreen' ) {
if( hasExtension() ) { // function that evaluates whether my extension is installed
window.postMessage( {
action: 'requestFullscreen'
}, 'http://privilegeduri' );
}
}
content.js
脚本:
function receiveMessage( e ) {
if( e.source === window && e.origin === 'http://privilegeduri' ) {
switch( e.data.action ) {
case 'requestFullscreen':
chrome.runtime.sendMessage( e.data );
break;
}
}
}
window.addEventListener( 'message', receiveMessage );
background.js
脚本:
function receiveMessage( message, sender, sendResponse ) {
if( sender.id === chrome.runtime.id && sender.url.match( /^http:\/\/privilegeduri/ ) ) {
switch( message.action ) {
case 'requestFullscreen':
browser.windows.get( sender.tab.windowId, {}, function( w ) {
console.log( w.state ); // outputs 'normal'
w.state = 'fullscreen';
console.log( w.state ); // outputs the expected value 'fullscreen'
} );
break;
}
}
}
chrome.runtime.onMessage.addListener( receiveMessage );
啊...我刚遇到 windows.update()
方法,当我尝试通过 updateInfo
参数对象设置 state
时,它 做了 通知:
Type error for parameter updateInfo (Property "state" is unsupported by Firefox) for windows.update
真可惜
现在是 2018 年,代码库发生了变化。如果您使用的是 Firefox Quantum,那么 windows.update() 现在可以按预期运行。这是一个简单的例子:
manifest.json:
{
"manifest_version": 2,
"name": "WootFullscreen",
"version": "9000.0.0.1",
"description": "Sends woot, gets fullscreen.",
"author": "Mekronid",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["messagesender.js"]
}
],
"background": {
"scripts": ["messagereciever.js"]
},
"permissions": ["tabs", "<all_urls>"]
}
messagesender.js:
browser.runtime.sendMessage({message: "woot"})
messagereciever.js:
browser.runtime.onMessage.addListener(listener)
async function listener(findWoot) {
console.log(findWoot.message)
var val = await browser.windows.getCurrent()
browser.windows.update(val.id, { state: "fullscreen"})
}