addon-page.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'stability': 'deprecated'
  7. };
  8. const { WindowTracker } = require('./deprecated/window-utils');
  9. const { isXULBrowser } = require('./window/utils');
  10. const { add, remove } = require('./util/array');
  11. const { getTabs, closeTab, getURI } = require('./tabs/utils');
  12. const { data } = require('./self');
  13. const { ns } = require("./core/namespace");
  14. const addonURL = data.url('index.html');
  15. const windows = ns();
  16. require("./util/deprecate").deprecateUsage(
  17. "The addon-page module is deprecated." +
  18. "In the new Firefox UI design all pages will include navigational elements;" +
  19. "once the new design ships, using the addon-page module will not have any effect."
  20. );
  21. WindowTracker({
  22. onTrack: function onTrack(window) {
  23. if (!isXULBrowser(window) || windows(window).hideChromeForLocation)
  24. return;
  25. let { XULBrowserWindow } = window;
  26. let { hideChromeForLocation } = XULBrowserWindow;
  27. windows(window).hideChromeForLocation = hideChromeForLocation;
  28. // Augmenting the behavior of `hideChromeForLocation` method, as
  29. // suggested by https://developer.mozilla.org/en-US/docs/Hiding_browser_chrome
  30. XULBrowserWindow.hideChromeForLocation = function(url) {
  31. return isAddonURL(url) || hideChromeForLocation.call(this, url);
  32. }
  33. },
  34. onUntrack: function onUntrack(window) {
  35. if (isXULBrowser(window))
  36. getTabs(window).filter(tabFilter).forEach(untrackTab.bind(null, window));
  37. }
  38. });
  39. function isAddonURL(url) {
  40. if (url.indexOf(addonURL) === 0) {
  41. let rest = url.substr(addonURL.length);
  42. return ((rest.length === 0) || (['#','?'].indexOf(rest.charAt(0)) > -1));
  43. }
  44. return false;
  45. }
  46. function tabFilter(tab) {
  47. return isAddonURL(getURI(tab));
  48. }
  49. function untrackTab(window, tab) {
  50. // Note: `onUntrack` will be called for all windows on add-on unloads,
  51. // so we want to clean them up from these URLs.
  52. let { hideChromeForLocation } = windows(window);
  53. if (hideChromeForLocation) {
  54. window.XULBrowserWindow.hideChromeForLocation = hideChromeForLocation.bind(window.XULBrowserWindow);
  55. windows(window).hideChromeForLocation = null;
  56. }
  57. closeTab(tab);
  58. }