gpt4 book ai didi

javascript - chrome.tabs.update() 重定向到 'chrome-extension://invalid/'

转载 作者:行者123 更新时间:2023-11-29 15:45:22 28 4
gpt4 key购买 nike

我编写了一个 chrome 扩展程序,它根据作为内容脚本注入(inject)的计时器的值重定向当前选项卡。后台脚本通过每隔一段时间轮询每个计时器来跟踪所有打开的选项卡的运行时间,如果在特定站点上花费的时间超过给定限制,则将事件选项卡重定向到插页式页面,并可以选择重置计时器并恢复以前的 URL。这是有效的,但是一旦计时器被重置一次,chrome.tabs.update() 会将事件选项卡重定向到 chrome-extension://invalid/。我不确定为什么,甚至不知道如何诊断这里发生的事情。

这是它应该如何工作的,一步一步。首先,后台脚本中的 if block 在时间到时被触发:

背景.js:

if (time_elapsed > time_limit) {
settings.restore_url = tab.url;
var timeup_url = chrome.extension.getURL('timeup.html');
update_icon("off");
chrome.tabs.update({url: timeup_url});
}

这会将当前选项卡的 url 保存到 settings 对象,从扩展目录中获取静态页面 timeup.html 的 URL,更新工具栏图标,并重定向当前选项卡到 timeup.html

timeup.html:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="bootstrap.css" type="text/css">
<style>
.hero-unit {
background-color: #ffffff;
text-align: center;
}
.icon {
vertical-align: middle;
}
.xlarge {
font-size: 20px;
}
</style>
<script src="timeup.js" type="text/javascript"></script>
<title>Tabminder</title>
</head>
<body>
<div class="container">
<div class="row-responsive">
<div class="hero-unit">
<h1>Don't get distracted!</h1>
<p>You've been browsing a timesink site for too long.</p>
<p>
<a class="btn btn-inverse btn-large xlarge" id="close-tabs"><img src="img/remove.png">
<span class="icon"> Close tab</span>
</a>
<a class="btn btn-danger btn-large xlarge" id="restart-timer"><img src="img/repeat.png">
<span class="icon"> Restart timer</span>
</a>
</p>
</div>
</div>
</div>
</body>
</html>

这很简单。连接到 'click' 事件监听器的两个按钮......

timeup.js:

document.addEventListener('DOMContentLoaded', function () {
document.getElementById("close-tabs").addEventListener('click', close_tabs);
document.getElementById("restart-timer").addEventListener('click', restart_timer);
});

var port = chrome.extension.connect();

function close_tabs () {
port.postMessage({close_tabs: true});
}

function restart_timer () {
port.postMessage({restart_timer: true});
}

#restart-timer 按钮调用 restart_timer(),它将消息发送回内容脚本以重定向选项卡:

背景.js:

// Listen for connections from content scripts
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
if (msg.name == "update") {
update_times(msg.update, port.sender.tab);
}

// Messages from timeup page:
if (msg.close_tabs === true) {
chrome.tabs.remove(port.sender.tab.id);
}

if (msg.restart_timer === true) {
var reset_hostname = get_location(settings.restore_url).hostname;
settings.elapsed_times[reset_hostname] = 0;
chrome.tabs.update({url: settings.restore_url});
}
});
});

这应该会重置存储在 settings 中的耗时并将选项卡重定向到 settings.restore_url。这第一次正常工作,但一旦计时器被重置,似乎重定向到 chrome-extension://invalid/。即使在第一次重置后,settings.restore_url 的正确值也会传递给 chrome.tabs.update(),因此我不确定重定向的来源.这里出了什么问题?以后如何诊断此类错误?

最佳答案

正如@RobW 指出的那样,添加您需要主机页面访问的文件 web_accessible_resources :

Manifest - Web Accessible Resources

An array of strings specifying the paths of packaged resources that are expected to be usable in the context of a web page. These paths are relative to the package root, and may contain wildcards.

...

These resources would then be available in a webpage via the URL chrome-extension://[PACKAGE ID]/[PATH], which can be generated with the extension.getURL method. Whitelisted resources are served with appropriate CORS headers, so they're available via mechanisms like XHR.

A navigation from a web origin to an extension resource will be blocked unless the resource is listed as web accessible.

(仅摘录)

关于javascript - chrome.tabs.update() 重定向到 'chrome-extension://invalid/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12359608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com