[英]Code runs fine on console but gives error when injected by browser extension(works on chrome not in firefox)
The extension injects the following code:该扩展注入以下代码:
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';
}
gives Uncaught SyntaxError: await is only valid in async function
but when runned on console manually it works.Await is used as suggested here给出Uncaught SyntaxError: await is only valid in async function
但是当手动在控制台上运行时它可以工作。按照此处的建议使用 Await
You can run the code manually in the console because Chrome DevTools support "Top-level await" .您可以在控制台中手动运行代码,因为Chrome DevTools 支持 "Top-level await" 。
Top-level await enables developers to use the await keyword outside of async functions.顶级 await 使开发人员可以在异步函数之外使用 await 关键字。 It acts like a big async function causing other modules who import them to wait before they start evaluating their body.它就像一个大的异步 function 导致导入它们的其他模块在开始评估他们的身体之前等待。
To fix your code, you can simply wrap your code with an async function and run that function.要修复您的代码,您可以简单地使用异步 function 包装您的代码并运行该 function。 Here is a possible implementation:这是一个可能的实现:
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();
More info about Top-level await .有关顶级 await 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.