我試圖將 Timeout 設定為我的擴展彈出視窗。我看到作業完成后,它不會自動關閉,直到單擊頁面上的某個位置。我試圖為自動關閉我的擴展彈出視窗設定超時。下面是我的代碼。
a.addEventListener("click", async () => {
button.style.backgroundColor = 'white';
document.getElementById("button").style.backgroundColor = 'white';
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: codeWork,
});
});
我遵循了許多可用的建議,但它拋出了Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not a allowed source of script in Content Security Pol 中顯示的錯誤 Uncaught EvalError: Refused to evaluate a string as JavaScript
請幫助我了解如何為我的彈出功能設定計時器。
還有我的 func:codeWork 回傳回應。回應可能包含錯誤。我想根據回應更改按鈕的顏色。怎么做 ?任何幫助都非常感謝!!!!
uj5u.com熱心網友回復:
這是第一個問題的答案。此示例彈出視窗將在 10 秒后自動關閉。
popup.js
const elmCounter = document.getElementById("counter");
counter();
async function counter() {
for (let i = 0; i < 10; i ) {
elmCounter.innerText = i;
await new Promise(r => setTimeout(r, 1000));
}
window.close();
}
popup.html
<!DOCTYPE html>
<html>
<body>
<div id="counter">
<script src="popup.js"></script>
</body>
</html>
清單.json
{
"name": "hoge",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_popup": "popup.html"
}
}
uj5u.com熱心網友回復:
這是第二個問題的答案。
popup.js
const elmExec = document.getElementById("exec");
elmExec.onclick = async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
await chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: func
});
}
const func = () => {
const color = ["red", "blue", "green"];
const getRandomInt = (max) => {
return Math.floor(Math.random() * max);
}
chrome.runtime.sendMessage({ color: color[getRandomInt(3)] });
}
chrome.runtime.onMessage.addListener((message) => {
elmExec.style.backgroundColor = message.color;
});
popup.html
<!DOCTYPE html>
<html>
<body>
<div id="counter">
<input type="button" id="exec" value="exec">
<script src="popup.js"></script>
</body>
</html>
清單.json
{
"name": "hogehoge",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"activeTab",
"scripting"
],
"action": {
"default_popup": "popup.html"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522456.html
