Chrome扩展-javascript将结果返回到弹出HTML



我正在开发chrome扩展,我不知道如何将popup.js中返回的javascript响应传递回popup.html(这样用户就可以看到结果(。我尝试将<div id="tabs"> </div>添加到popup.html中,认为这会动态地返回function(tabs)结果,但它没有,非常感谢任何见解。

popup.html

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="myButton.css">
</head>
<body style="background-color:Gray;">
<a href="#" class="myButton">Scan</a>
<script src=popup.js></script>
<div id="tabs"> </div>
</body>
</html>

popup.js

chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
var tabURL = tabs[0].url;
var api = "https://api.us-east-2.amazonaws.com..."
console.log(tabURL);
console.log(api)
userdata = {"url":tabURL}
console.log(userdata)

fetch(api, {
method: 'POST',
body: JSON.stringify(userdata),
})
.then((resp) => resp.json())
.then(function(response) {
console.info('fetch()', response);
return response;
}); 
});

您可以使用popup.js.将元素插入到popup.html中

例如

function addSpan(response) {
let tabDiv = document.getElementById('tabs');
let newSpan = document.createElement('span');
newSpan.innerText = "Test123".
// newSpan.innerText = response;

tabDiv.append(newSpan);
}

最新更新