gpt4 book ai didi

javascript - Chrome 扩展 - 将 URL 从 popup.js 传递到 Firebase

转载 作者:行者123 更新时间:2023-12-03 06:56:14 25 4
gpt4 key购买 nike

我正在使用 chrome.tabs.query 获取当前事件 URL,然后我想将其传递到 firebase 中的后端。在下面的代码中:

function geturl() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var tabURL = tabs[0].url;
document.getElementById("demo").innerHTML = tabURL; //The line I changed to pass the URL to html.
});

fbase.set({
title: tabURL, // If I set this to title: "test", it works
});

}

现在,当我触发此操作时,没有任何反应,因此我检查并填写了测试,这确实有效。这让我觉得 Chrome 不希望我这样做。我可以将 URL 传递到我的 popup.html,它会显示它,但无法从 popup.js 发送它。有解决方法吗?还是我需要使用 popup.html

干杯

最佳答案

因为 chrome.tabs.query 是异步的。

fbase.set 被调用,而 chrome.tabs.query 尚未完成执行。

您必须将 fbase.set 放入 chrome.tabs.query 的回调中。

function getUrl() {

chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {

var tabURL = tabs[0].url;

fbase.set({title: tabURL});
});
}

Asynchronous vs. synchronous methods

关于javascript - Chrome 扩展 - 将 URL 从 popup.js 传递到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260831/

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