简体   繁体   English

代码在控制台上运行良好,但在通过浏览器扩展注入时出错(适用于 chrome 而不是 Firefox)

[英]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

here is a video demo of the problem.这是该问题的视频演示。

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.

相关问题 在Chrome中正常运行时,代码无法在Firefox中运行 - Code not working in firefox while runs fine in chrome 代码可以在Chrome控制台中正常运行,但不能在Chrome扩展程序中运行 - Code running fine in Chrome Console but not in Chrome extension 代码可以在控制台中工作,但不能在Chrome扩展程序中工作吗? - Code works in console but not in Chrome extension? 灯箱代码在 Chrome/Firefox 中运行良好,但在 IE 中无法运行 - Lightbox code works fine in Chrome/Firefox but not in IE 意外警报打开错误。 “browser.switchTo()。alert()。accept();”在Firefox中运行正常,但在Jenkins运行时不适用于Chrome - Unexpected Alert Open Error. “browser.switchTo().alert().accept();” works fine in firefox but not in Chrome when running through Jenkins 此代码适用于Chrome,Firefox但不适用于IE - This code works fine in Chrome, Firefox but not in IE javascript出现IE错误,在Firefox和Chrome上正常运行 - IE error with javascript that works fine on Firefox and Chrome 在Firefox中可以正常工作,但在chrome中不能正常工作 - Works fine in Firefox but not in chrome 为什么当 URL 为空时,chrome 控制台中的 fetch() 请求失败? . 但在 Firefox 中工作正常 - Why fetch() request fails in chrome console when URL is empty ? . But Works fine in Firefox 在 DIV 中选择 P 在浏览器控制台中会出错,但在 JsFiddle 中工作正常。 为什么? - Selecting a P inside a DIV gives error in browser console but in JsFiddle works fine. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM