tabs.executeScript-如何确定是否已注入内容脚本? [英] tabs.executeScript - how can I tell if content script was being injected?

查看:131
本文介绍了tabs.executeScript-如何确定是否已注入内容脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用browser.tabs.executeScript以编程方式将内容脚本注入网站.这将返回一个Promise,但是在被拒绝的情况下,似乎无法区分以下两种情况:

I inject content scripts into websites programmatically using browser.tabs.executeScript. This will return a Promise, but in case of its rejection there seems to be no way of differentiating between the following 2 cases:

  1. 无法注入内容脚本(例如,缺少主机 权限)
  2. 在脚本 update 解析 end:update 时发生错误(但脚本已被注入)
  1. The content script couldn't be injected (i.e. for missing host permission)
  2. An error occured on script updateparsingend:update(but the script was being injected)

我只对脚本是否被注入感兴趣.

I'm only interested in whether the script was being injected or not.

传递给catch的参数是一个Error对象. catch(e => console.log(e.toString())将输出错误消息,这可能是注入失败的原因(即Missing host permission),也可能是 update 读取脚本 end:update 时发生的错误. >.

The argument passed to the catch is an Error object. catch(e => console.log(e.toString()) will output the error message, which can either be a reason for an injection failure (i.e. Missing host permission) or an error that occurred updatereading the scriptend:update.

    browser.tabs.executeScript(tabId, {
        file: '../path/to/content-script.js',
        frameId: 0,
        runAt: 'document_idle'
    })
        .catch(e => console.log(e.toString()));

例如,如果内容脚本如下:

So, for example if the content script is as follows:

    window.document.body.addEventListener('click', e => console.log('clicked body'), false);
    bla.bla();

然后,由于未定义bla.bla,因此Promise被拒绝了-但是脚本已成功注入.

then the Promise is being rejected, since bla.bla is undefined - but the script was being injected successfully.

如果无法注入内容脚本,我想用相应的错误消息通知用户. 但是,当发生更新错误时,该错误与是否可以注入脚本 end:update 无关,而在注入脚本时,我不想通知用户,但请保持安静. 有没有办法区分这两种情况?

In case the content script couldn't be injected I'd like to notify the user with the corresponding error message. But when an error occurred updatethat is unrelated to whether the script could be injectedend:update, while the script was being injected, I don't want to notify the user, but handle it silently. Is there a way to differentiate between those 2 cases?

编辑:我想出了一个间接解决方案:万一返回的Promise被拒绝,我尝试向内容脚本发送一条消息.如果失败,则后台脚本知道"没有注入内容脚本->通知用户.

I came up with an indirect solution: In case the returned Promise was rejected I try to send a message to the content script. If this fails then the background script "knows" that no content script was being injected -> notify user.

推荐答案

这是Promise的工作原理....

This is how the Promise works....

browser.tabs.executeScript(tabId, {
  file: '../path/to/content-script.js',
  frameId: 0,
  runAt: 'document_idle'
})
.catch(e => console.log(e.toString()));

如果tabs.executeScript无法注入,则上面的

catch将捕获错误.如果JS文件有解析错误(无效的JS),则在解析文件以便注入时,它也可能会显示一些错误.与之后的 ../path/to/content-script.js 无关.

catch in above will catch errors if the tabs.executeScript failed to inject. It may also show some errors when parsing the file in order to inject, if the JS file has parsing errors (invalid JS). It has nothing to do with what '../path/to/content-script.js' will be doing afterwards.

因此,一旦注入,就实现了Promise以上的要求.

So once it was injected, then above Promise is fulfilled.

如果注入的脚本具有 sync 返回,则tabs.executeScript可以通过then()将其接收,例如

If the injected script has a sync return, then it can be received by the tabs.executeScript via then() e.g.

browser.tabs.executeScript(tabId, {
  file: '../path/to/content-script.js',
  frameId: 0,
  runAt: 'document_idle'
})
.then(result => {})
.catch(e => console.log(e.toString()));

如果异步功能(例如.addEventListener)将在以后发生,则不会返回tabs.executeScript

In case of async functions such as .addEventListener which will happen later, then is nothing returned to tabs.executeScript

要捕获内容脚本中的错误,您可以在内容脚本中生成错误消息,也可以将消息发送到后台脚本,即sendMessage& onMessage.addListener

To catch errors in the content scripts, you can generate the error message within the content script or send a message to background script i.e. sendMessage & onMessage.addListener

tabs.executeScript ()
一个承诺,将通过一系列对象来实现, 表示在每个注入的帧中脚本的结果.
脚本的结果是最后计算的语句,即 类似于将要输出的内容(结果,没有任何console.log() 输出)(如果您在Web控制台中执行了脚本).例如, 考虑这样的脚本:

tabs.executeScript()
A Promise that will be fulfilled with an array of objects, representing the result of the script in every injected frame.
The result of the script is the last evaluated statement, which is similar to what would be output (the results, not any console.log() output) if you executed the script in the Web Console. For example, consider a script like this:

var foo='my result';foo;

browser.tabs.executeScript(tabId, {
  file: '../path/to/content-script.js',
  frameId: 0,
  runAt: 'document_idle'
})
.then(result => {
  // result is returned by the Promise
  if (result === []) { 
    // it was fine but there was nothing to return
  }
  else if (result[0]) {
    // result[0] is return from the promise
  }
})
.catch(e => console.log(e.toString()));

现在,如果您想要退货(必须同步,否则必须将其绑定到另一个Promise),则从'../path/to/content-script.js'中退货.

Now if you want a return (it must be sync or else you have to tie it to another Promise), return something from '../path/to/content-script.js'

这篇关于tabs.executeScript-如何确定是否已注入内容脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆