[英]Code runs fine on console but gives error when injected by browser extension(works on chrome not in firefox)
該擴展注入以下代碼:
var forbidden;
console.log("url: ",window.location.href);
async function fetchData(link) {
return fetch(link)
.then(response =>response.text())
.then(text => text.split(/\r|\n/))
}
forbidden=await fetchData(`https://truemysterious98.github.io/Page/uploads/txt/url/url.txt`);
for (var i =0;i<forbidden.length;i++){
console.log(forbidden[i]);
if(window.location.href.includes(forbidden[i])) window.location= 'https://truemysterious98.github.io/Page/t/m.html';
}
給出Uncaught SyntaxError: await is only valid in async function
但是當手動在控制台上運行時它可以工作。按照此處的建議使用 Await
您可以在控制台中手動運行代碼,因為Chrome DevTools 支持 "Top-level await" 。
頂級 await 使開發人員可以在異步函數之外使用 await 關鍵字。 它就像一個大的異步 function 導致導入它們的其他模塊在開始評估他們的身體之前等待。
要修復您的代碼,您可以簡單地使用異步 function 包裝您的代碼並運行該 function。 這是一個可能的實現:
async function fetchData(link) {
return fetch(link)
.then((response) => response.text())
.then((text) => text.split(/\r|\n/));
}
async function main() {
console.log("url: ", window.location.href);
var forbidden = await fetchData(
`https://truemysterious98.github.io/Page/uploads/txt/url/url.txt`
);
for (var i = 0; i < forbidden.length; i++) {
console.log(forbidden[i]);
if (window.location.href.includes(forbidden[i]))
window.location = "https://truemysterious98.github.io/Page/t/m.html";
}
}
main();
有關頂級 await 的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.