如何防止 Firefox 在启动时显示我的扩展程序的侧边栏?

发布于 2024-08-09 19:40:07 字数 486 浏览 5 评论 0原文

如果我自己的侧边栏在 Firefox 关闭时保持打开状态,则在启动时会再次显示。我发现这是不可取的,并且宁愿它保持隐藏状态直到手动打开。是否可以阻止这种情况发生?

我尝试将这段代码添加到我的扩展程序的初始化函数中,以便在侧边栏确实出现时关闭它:

toggleSidebar("mySidebar", false);

这似乎不太一致 - 它似乎忽略了false 参数并且只是切换侧边栏!当它正常工作时,它会产生不需要的副作用 - 我需要打开和关闭侧边栏一次,然后它才会显示任何内容。很奇怪,但我认为 Firefox 对侧边栏可见性的部分看法已经不同步。

似乎其他人在 MozillaZine 论坛 上也遇到了同样的问题。

If my own sidebar is left open when Firefox is closed, it is shown again on startup. I find this undesirable and would rather it remain hidden until opened manually. Is it possible to stop this happening?

I tried adding this bit of code to my extension's initialisation function in order to close the sidebar if it does appear:

toggleSidebar("mySidebar", false);

This doesn't seem too work too consistently - it seems to ignore the false parameter and just toggles the sidebar! When it does work correctly it has unwanted side effects - I need to open and close the sidebar once before it will show any content. Weird, but I assume part of Firefox's view as to the sidebar's visibility has got out of sync.

It seems others are having the same trouble on the MozillaZine forums.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦冥 2024-08-16 19:40:07

您还可以向关闭过程添加观察者,并关闭那里的侧边栏。我遇到了和你一样的问题,最终选择了这条路线,因为我们已经设置了一个观察者来进行其他清理工作。

在主覆盖层初始化期间,代码如下所示:

    var current = this;
    var quitObserver = {
   QueryInterface: function(aIID) {
      if (aIID.equals(Components.interfaces.nsIObserver) || aIID.equals(Components.interfaces.nsISupports)) {
         return this;
      }
      throw Components.results.NS_NOINTERFACE;
   },
   observe: function(aSubject,aTopic,aData ) {
      var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
      var sidebar  = mainWindow.document.getElementById("sidebar");
      if (sidebar.contentWindow.location.href == "chrome://daisy/content/sidebar.xul") {
         mainWindow.toggleSidebar('openDaisySidebar', false);
      }
   }
};

setTimeout(function() {
   var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
   observerService.addObserver(quitObserver,"quit-application-granted",false);
},2000);

希望有帮助。

You could also add an observer to the shutdown process, and close the sidebar there. I had the same issue as you, and ended up going that route, since we already had an observer set up to do some other cleanup.

The code looks like this, during the initialization of the main overlay:

    var current = this;
    var quitObserver = {
   QueryInterface: function(aIID) {
      if (aIID.equals(Components.interfaces.nsIObserver) || aIID.equals(Components.interfaces.nsISupports)) {
         return this;
      }
      throw Components.results.NS_NOINTERFACE;
   },
   observe: function(aSubject,aTopic,aData ) {
      var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
      var sidebar  = mainWindow.document.getElementById("sidebar");
      if (sidebar.contentWindow.location.href == "chrome://daisy/content/sidebar.xul") {
         mainWindow.toggleSidebar('openDaisySidebar', false);
      }
   }
};

setTimeout(function() {
   var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
   observerService.addObserver(quitObserver,"quit-application-granted",false);
},2000);

Hope that helps.

倾城月光淡如水﹏ 2024-08-16 19:40:07

toggleSidebar("mySidebar", false); 不起作用,因为第二个参数表示“forceOpen”而不是打开/关闭标志。

正如有人在 Mozillazine 上回答的那样,这是因为 Firefox 只是在会话之间保留侧边栏状态。您确定您需要在这里有所不同吗?

以下是 Firefox 中在启动时打开侧边栏的代码:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#999
(当您稍后访问此内容时,这些行可能会关闭 - 这是最新版本源代码的链接)

1000     let box = document.getElementById("sidebar-box");
1001     if (box.hasAttribute("sidebarcommand")) {

这里是 sidebarcommand 属性被保留的地方:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content /browser.js#1422

1373 function BrowserShutdown()
...
1426   if (!enumerator.hasMoreElements()) {
1427     document.persist("sidebar-box", "sidebarcommand");

我建议尝试这个:
如果当前窗口是最后一个窗口并且侧边栏已打开,

  • 请在 BrowserShutdown 之前关闭侧边栏(它是 注册为窗口的卸载处理程序
  • 欺骗 BrowserShutdown 保留正确的属性(对应于“侧边栏未打开”的情况)
  • 或在 BrowserShutdown 之后用您需要的值覆盖持久值。

如果您提出可行的解决方案,请与其他人分享,最好在 MDC 上分享:https:/ /developer.mozilla.org/En/Code_snippets/Sidebar

toggleSidebar("mySidebar", false); doesn't work because the second param means "forceOpen" not an open/close flag.

As someone answered on Mozillazine this is because Firefox just preserves sidebar state across sessions. Are you sure you need to be different here?

Here's the code in Firefox that opens the sidebar on startup:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#999
(the lines may be off when you access this later - it's the link to the latest version of the source)

1000     let box = document.getElementById("sidebar-box");
1001     if (box.hasAttribute("sidebarcommand")) {

And here's where the sidebarcommand attribute gets persisted:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#1422

1373 function BrowserShutdown()
...
1426   if (!enumerator.hasMoreElements()) {
1427     document.persist("sidebar-box", "sidebarcommand");

I suggest trying this:
If current window is the last one and your sidebar is open,

  • either close the sidebar before BrowserShutdown (it's registered as an unload handler for the window)
  • trick the BrowserShutdown to persist the right attributes (corresponding to the "sidebar not opened" case)
  • or after BrowserShutdown overwrite the persisted values with the ones you need.

If you come up with the working solution, please share it with others, preferably on MDC: https://developer.mozilla.org/En/Code_snippets/Sidebar

浅笑依然 2024-08-16 19:40:07

请参阅如何在 Firefox 中关闭侧边栏 了解如何在关机时关闭它以及以防万一在关机时关闭它。

See the answers at How to close a sidebar in firefox for how to close it at shutdown and, just in case, at shutdown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文