需要从后台脚本调用alert()的解决方法 [英] Need workaround for calling alert() from background script

查看:126
本文介绍了需要从后台脚本调用alert()的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google的Chrome浏览器中允许从后台脚本调用alert(),但在将我的Chrome扩展程序移植到的Firefox(WebExtensions)中却不允许.

Calling alert() from the background script is allowed in Google's Chrome, but not in Firefox (WebExtensions) which I am porting my Chrome extension to.

因此,我需要一种变通方法来启动警报对话框.除了警告"对话框外,我没有其他要求.

So, I need a workaround to get an alert dialog up. I'm not asking for anything else other than THE alert dialog.

将消息发送到内容脚本以调用alert()是不够的,因为在需要警报调用时可能没有加载任何内容脚本.

Sending a message to a content script to call alert() will not suffice since no content scripts may be loaded at the time the alert call is needed.

推荐答案

我的解决方法是将警报代码保存在类似以下的字符串中:

My workaround is to save the alert code in a string like:

var alertWindow = 'alert(message)';

并让后台脚本注入如下代码:

and let the background script inject that code like:

browser.tabs.executeScript({code : alertWindow});

这篇关于需要从后台脚本调用alert()的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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