Where did you install Firefox from? Help Mozilla uncover 3rd party websites that offer problematic Firefox installation by taking part in our campaign. There will be swag, and you'll be featured in our blog if you manage to report at least 10 valid reports!

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

Learn More

JavaScript in firefox.cfg can't access `document`

  • 3 个回答
  • 0 人有此问题
  • 1 次查看
  • 最后回复者为 jazali

more options

As I understand, `autoconfig.js` (with `firefox.cfg`) can be used to have JavaScript run during startup to control Firefox's UI elements.

I have set up `autoconfig.js` in my `/usr/lib/firefox-developer-edition/defaults/pref` folder (I'm on EndeavourOS Linux). As instructed here, I've already put the two required files, `autoconfig.js` and `firefox.cfg` in their correct folders (`/usr/lib/firefox-developer-edition/defaults/pref` and `/usr/lib/firefox-developer-edition` in my machine).

The file `firefox.cfg` contains the following code:

 //
 (() => {
   var tabbar = document.getElementById("TabsToolbar");
 })();


However, every time I run Firefox, this message will appear:

 "Failed to read the configuration file. Please contact your system administrator."


If I empty out the `firefox.cfg` file, Firefox will run normally without showing the error message. It will also run normally if I just have something like this in the file:

 //
 (() => {
   var testVar = "";
 })();


I don't understand why the JavaScript code can't access the Firefox UI elements through `document` or `window.document`. If `autoconfig.js` isn't build for that, what should I use to change my browser's UI elements through JavaScript?

Thank you.

As I understand, `autoconfig.js` (with `firefox.cfg`) can be used to have JavaScript run during startup to control Firefox's UI elements. I have set up `autoconfig.js` in my `/usr/lib/firefox-developer-edition/defaults/pref` folder (I'm on EndeavourOS Linux). As instructed [https://support.mozilla.org/en-US/kb/customizing-firefox-using-autoconfig here], I've already put the two required files, `autoconfig.js` and `firefox.cfg` in their correct folders (`/usr/lib/firefox-developer-edition/defaults/pref` and `/usr/lib/firefox-developer-edition` in my machine). The file `firefox.cfg` contains the following code: // (() => { var tabbar = document.getElementById("TabsToolbar"); })(); However, every time I run Firefox, this message will appear: "Failed to read the configuration file. Please contact your system administrator." If I empty out the `firefox.cfg` file, Firefox will run normally without showing the error message. It will also run normally if I just have something like this in the file: // (() => { var testVar = ""; })(); I don't understand why the JavaScript code can't access the Firefox UI elements through `document` or `window.document`. If `autoconfig.js` isn't build for that, what should I use to change my browser's UI elements through JavaScript? Thank you.
已附加屏幕截图

由jazali于修改

被采纳的解决方案

Note that you need a lot more code to be able to access a browser window (i.e. add an observer that is triggered when you open a window).


// start autoconfig.cfg with a comment line
try {
 function ConfigJS() { Services.obs.addObserver(this, 'chrome-document-global-created', false); }
 ConfigJS.prototype = {
  observe: function (aSubject) { aSubject.addEventListener('DOMContentLoaded', this, {once: true}); },
  handleEvent: function (aEvent) {
   let document = aEvent.originalTarget;
   let window = document.defaultView;
   let location = window.location;

   if (/^(chrome:(?!\/\/(global\/content\/commonDialog|browser\/content\/webext-panels)\.x?html)|about:(?!blank))/i.test(location.href)) {
    if (window._gBrowser) { //place your code here
       var tabbar = window.document.getElementById("TabsToolbar");
    }  // gBrowser
   }   // location
  }    // handleEvent
 };    // prototype
 if (!Services.appinfo.inSafeMode) { new ConfigJS(); }
} catch(e) {Cu.reportError(e);}

定位到答案原位置 👍 0

所有回复 (3)

more options

Did you disable the sandbox via autoconfig.js ?

Content of autoconfig.js:

//start autoconfig.js with a comment line
pref("general.config.filename", "autoconfig.cfg");
pref("general.config.obscure_value", 0);
pref("general.config.sandbox_enabled", false);

有帮助吗?

more options

选择的解决方案

Note that you need a lot more code to be able to access a browser window (i.e. add an observer that is triggered when you open a window).


// start autoconfig.cfg with a comment line
try {
 function ConfigJS() { Services.obs.addObserver(this, 'chrome-document-global-created', false); }
 ConfigJS.prototype = {
  observe: function (aSubject) { aSubject.addEventListener('DOMContentLoaded', this, {once: true}); },
  handleEvent: function (aEvent) {
   let document = aEvent.originalTarget;
   let window = document.defaultView;
   let location = window.location;

   if (/^(chrome:(?!\/\/(global\/content\/commonDialog|browser\/content\/webext-panels)\.x?html)|about:(?!blank))/i.test(location.href)) {
    if (window._gBrowser) { //place your code here
       var tabbar = window.document.getElementById("TabsToolbar");
    }  // gBrowser
   }   // location
  }    // handleEvent
 };    // prototype
 if (!Services.appinfo.inSafeMode) { new ConfigJS(); }
} catch(e) {Cu.reportError(e);}

有帮助吗?

more options

Hi. Thank you very much for the answer! My script can now access `document` thanks to your code. But I'm still confused on how to implement the following into the snipped you provided:

 var tabbar = window.document.getElementById("TabsToolbar");
 function showHideTabbar() {
   var sidebarBox = window.document.getElementById("sidebar-box");
   var sidebarTST = sidebarBox.getAttribute("sidebarcommand");
   if (!sidebarBox.hidden && sidebarTST === "treestyletab_piro_sakura_ne_jp-sidebar-action") {
     tabbar.style.visibility = "collapse";
   }
   else {
     tabbar.style.visibility = "visible";
   }
 }
 var observer = new MutationObserver(showHideTabbar);
 observer.observe(window.document.getElementById("sidebar-box"), { attributes: true, attributeFilter: ["sidebarcommand", "hidden"] });


Its purpose is to hide the tab strip on top when Tree Style Tab is open on the sidebar.

I found that it worked when I ran it through the Browser Toolbox. But it had no effect when I put it into the `if (window._gBrowser) {}` scope. I'm not too familiar with JavaScript, so I can't quite figure out what's wrong.

由jazali于修改

有帮助吗?

我要提问

您需要登录才能回复。如果您还没账号,可以提出新问题