In a Firefox add-on built with the Add-on SDK, how can I disable and re-enable JavaScript for a single tab?
Default hotkey (shortcut) to toggle JS engine on and off is "Alt + Shift + L." Still, there is no UI to edit this default shortcut.
Press Ctrl + Shift + P (Windows, Linux) or Command + Shift + P (macOS) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled.
Pressing ESC stops script execution.
The SDK itself doesn't provide this functionality, you will have to work with the XUL directly. What you need to do is accessing the docShell
property of the XUL <browser>
element corresponding to the tab. The docshell has an allowJavascript
property that lets you switch JavaScript on and off. Something like this should work:
var window = require("window-utils").activeBrowserWindow;
var tabBrowser = window.gBrowser;
var browser = tabBrowser.selectedBrowser; // or: tabBrowser.browsers[n]
browser.docShell.allowJavascript = false;
Unfortunately, it doesn't seem possible to take a Tab
object and find the corresponding XUL element - you have to work with the XUL window from the start.
Relevant documentation:
window-utils
package (the properties activeWindow
/activeBrowserWindow
are undocumented for some reason).<tabbrowser>
element<browser>
elementnsIDocShell
interfaceIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With