gpt4 book ai didi

javascript - 获取选项卡 ID 或 URL 并使用 getCurrent 显示它

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:26 25 4
gpt4 key购买 nike

这是我的内容script.js和background.html以及错误

contentscript.js

chrome.extension.sendRequest({"type":""}); 

背景.html

<script type="text/javascript" charset="utf-8">

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
chrome.tabs.getCurrent(function(tab){var urls = tab.url;console.log(urls);});

});

</script>

background.html 控制台上的错误消息

background.html:7 Uncaught TypeError: Cannot read property 'url' of undefined

这应该是一个简单的改变,但我卡住了。

最佳答案

您不能调用 get getCurrent,因为您不是从选项卡上下文中调用它。该文档说您也不能使用它。如果您的扩展程序创建了一个选项卡,您只能使用它。您想要获取其标签 ID 以将其与扩展名 message passing 一起使用.后台页面没有tab。

Gets the tab that this script call is being made from. May be undefined if called from a non-tab context (for example: a background page or popup view).

如果你想得到当前的tab id,你可以使用chrome.tabs.getSelected这将允许您获取其 URL 和 ID。

关于javascript - 获取选项卡 ID 或 URL 并使用 getCurrent 显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4836364/

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