在Firefox扩展中阻止脚本执行? [英] Block script execution in Firefox extension?

查看:54
本文介绍了在Firefox扩展中阻止脚本执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能正在寻找一个古老问题的答案,但我想阻止脚本执行.在我的用例中,阻止浏览器是可以接受的.

Probably looking for an answer to an age-old question, but I would like to block script execution. In my use-case blocking the browser is acceptable.

此外,在我的用例中,我尝试通过Firefox扩展程序执行此操作,这意味着我的代码是在浏览器环境中运行的"Chrome代码".

Also, in my use-case I am trying to do this from a Firefox extension, which means my code is "Chrome code", running in the browser environment.

这可以通过使用模式窗口轻松地完成,然后以编程方式关闭该窗口.因此,这表明存在阻塞机制.

This can easily be done by using a modal window, then programmatically closing the window. So this demonstrates that there is a blocking mechanism that exists.

有什么方法可以在不实际创建或打开模态窗口的情况下实现模态阻塞?可以利用某种方式来使用模态窗口的阻止机制吗?

Is there any way to achieve modal blocking without actually creating or opening the modal window? Some way to tap into the blocking mechanism used for modal windows?

我已经在这个主题上进行了很多搜索,但无济于事.

I've done a lot of searching on this subject, but to no avail.

推荐答案

使用nsIProcess可以阻止线程.

Using nsIProcess you can block the thread.

您可以创建一个具有sleep或usleep方法或等效方法的可执行文件.然后同步运行该进程(nsIProcess.run),并将阻塞参数设置为true.

You can create an executable which has a sleep or usleep method or equivalent. Then run the process synchronously (nsIProcess.run) and set blocking argument to true.

当然,为实现可移植性,您将需要创建一个适用于您希望支持的每个平台的可执行文件,并提供识别代码.

Of course for portability you will need to create an executable appropriate for each platform you wish to support, and supply code for discrimination.

基本代码如下.我已经在'nix(Mac OS X)上验证了此代码是否可以使用仅使用sleep .03:

Basic code is something like the following. I have verified on 'nix (Mac OS X) this code to work, using a bash script with only the line sleep .03:

let testex = Components.classes["@mozilla.org/file/local;1"]
                    .createInstance(Components.interfaces.nsIFile);

testex.initWithPath("/Users/allasso/Desktop/pause.sh");

let process = Components.classes["@mozilla.org/process/util;1"]
                    .createInstance(Components.interfaces.nsIProcess);

process.init(testex);
let delay = 30;  // convert this to milliseconds in the executable
process.run(true,[delay],1);  // `run` method runs synchronously, first arg says to block thread

在扩展名中,您可能希望使nsIFile文件对象更具可移植性:

In an extension you probably would want to make your nsIFile file object more portable:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
let testex = FileUtils.getFile("ProfD",["extension@moz.org","resources","pause.sh"]);

当然要记住Javascript基本上是单线程的,因此,除非您阻止使用Web Workers生成的线程,否则您将在睡眠期间冻结整个UI(就像您打开模式窗口一样)

Of course keep in mind that Javascript is basically single-threaded, so unless you are blocking a thread spawned using Web Workers you will be freezing the entire UI during the sleep period (just like you would if you opened a modal window).

参考文献:

https://developer.mozilla. org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIProcess

https://developer.mozilla. org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile

https://developer.mozilla.org/zh-美国/附加组件/代码段/File_I_O#Getting_special_files

https://developer.mozilla.org/zh- US/docs/Web/API/Web_Workers_API/basic_usage

这篇关于在Firefox扩展中阻止脚本执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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