Javascript 代码在控制台上运行良好,但在浏览器扩展注入时出错(在chrome上工作,而不是在firefox中)

Javascript 代码在控制台上运行良好,但在浏览器扩展注入时出错(在chrome上工作,而不是在firefox中),javascript,browser-extension,Javascript,Browser Extension,扩展注入以下代码: 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://truemy

扩展注入以下代码:

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';
}
var禁止;
log(“url:,window.location.href”);
异步函数获取数据(链接){
返回获取(链接)
.then(response=>response.text())
.然后(text=>text.split(/\r |\n/))
}
禁止=等待获取数据(`https://truemysterious98.github.io/Page/uploads/txt/url/url.txt`);

对于(var i=0;i您可以在控制台中手动运行代码,因为

顶级await使开发人员能够在外部使用await关键字 异步函数。它的行为就像一个大的异步函数,导致其他 导入它们的模块在开始评估它们的 身体

要修复代码,只需使用异步函数包装代码并运行该函数即可。以下是一种可能的实现:

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();
异步函数fetchData(link){ 返回获取(链接) .然后((response)=>response.text()) .然后((text)=>text.split(/\r\n/); } 异步函数main(){ log(“url:,window.location.href”); var probled=等待获取数据( `https://truemysterious98.github.io/Page/uploads/txt/url/url.txt` ); 对于(变量i=0;i

有关的更多信息。

错误似乎很清楚:您不能在
异步
功能之外使用
await
。浏览器控制台环境有些怪异,因此很难将其用于此类测试。它是否也会停在控制台中?按照建议使用await