Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension Chrome扩展在根目录下创建html页面_Google Chrome Extension - Fatal编程技术网

Google chrome extension Chrome扩展在根目录下创建html页面

Google chrome extension Chrome扩展在根目录下创建html页面,google-chrome-extension,Google Chrome Extension,当前,我的扩展在以下位置打开一个网页: 铬-extension://bpmcncockdffdifceihffkimpfbobjhn/dist/webpage.html 如何使webpage.html被省略,并使以下url: 铬-extension://bpmcncockdffdifceihffkimpfbobjhn 立即指向网页 Chrome extensions页面被视为直接的文件URL,而不是服务器页面,因此像index.html这样的目录没有默认的处理程序 持久化背景脚本 如果您的后台脚

当前,我的扩展在以下位置打开一个网页:

铬-extension://bpmcncockdffdifceihffkimpfbobjhn/dist/webpage.html

如何使webpage.html被省略,并使以下url:

铬-extension://bpmcncockdffdifceihffkimpfbobjhn


立即指向网页

Chrome extensions页面被视为直接的文件URL,而不是服务器页面,因此像
index.html
这样的目录没有默认的处理程序

持久化背景脚本 如果您的后台脚本在manifest.json中没有
“persistent”:false
,您可以使用API将此类假URL重定向到真实URL:

manifest.json:

“权限”:[“webRequest”],
“背景”:{
“执着”:对,
“脚本”:[“background.js”]
}
background.js:

chrome.webRequest.onBeforeRequest.addListener(e=>{
返回{redirectUrl:chrome.runtime.getURL('/dist/webpage.html');
}, {
URL:[chrome.runtime.getURL('/')],
类型:[“主框架”],
},[“阻塞]);
事件页背景脚本 如果您的后台脚本在manifest.json中有
“persistent”:false
,则可以使用API将错误页面重定向到真实页面。唯一令人讨厌的是,选项卡的历史记录中将有一个虚拟项,历史记录返回按钮将处于活动状态(但它当然不会工作,因为它将再次被重定向)

manifest.json:

“权限”:[“Web导航”],
“背景”:{
“坚持”:假,
“脚本”:[“background.js”]
}
background.js:

chrome.webNavigation.onerrorOccursed.addListener({tabId})=>{
update(tabId,{url:'/dist/webpage.html'});
}, {
网址:[{
urlEquals:chrome.runtime.getURL('/'),
}],
});
Chrome beta/dev/Canary中也有但仅限于此。为了好玩,可以将WebRequestAPI与非持久性事件页面一起使用,但这是有漏洞的(您必须通过在后台页面中打开iframe中的消息连接来防止卸载事件页面),并且违背了事件页面的目的,所以我不在这里展示