Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable JavaScript in a single Firefox tab

In a Firefox add-on built with the Add-on SDK, how can I disable and re-enable JavaScript for a single tab?

like image 254
jd. Avatar asked Mar 13 '12 15:03

jd.


People also ask

How do I toggle JavaScript in Firefox?

Default hotkey (shortcut) to toggle JS engine on and off is "Alt + Shift + L." Still, there is no UI to edit this default shortcut.

How do I temporarily disable JavaScript?

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.

How do I stop scripts from running in Firefox?

Pressing ESC stops script execution.


1 Answers

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> element
  • nsIDocShell interface
like image 172
Wladimir Palant Avatar answered Sep 24 '22 07:09

Wladimir Palant