summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/addon-tab/lib/addon-tab.js
blob: 0236161f79f37797c30d55e3af3c537a18d04585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
  'stability': 'experimental'
};

const { WindowTracker } = require('sdk/deprecated/window-utils');
const { isXULBrowser } = require('sdk/window/utils');
const { add, remove } = require('sdk/util/array');
const { getTabs, closeTab, getURI, getTabURL, getBrowserForTab } = require('sdk/tabs/utils');
const { data } = require('sdk/self');
const { ns } = require("sdk/core/namespace");

const tabs = require("sdk/tabs");

const { defer } = require("sdk/lang/functional");

// store list of addon URLs to hide the navigation bar from
// and to add proper style.
// the url is used as the key for lookup efficiency.
// Objects contained are: {[url]: { [styles] }}
let addonTabs = {};

const windows = ns();

/* Add tab to a list of URL/styles. Can be called
 * from other scripts */
let addTabToList = function (options) {

  options = options || {};
  let url;

  if (options.url) {
    addonTabs[options.url] = {};
  }
  else {
    throw new Error("No url provided for the AddonTab page");
  }

  if (options.tabStyle)
    addonTabs[options.url] = options.tabStyle;
  else
    addonTabs[options.tabStyle] = null;

};

exports.removeAddonTab = function (url) {
  if (url && url in addonTabs)
    delete addonTabs[url];
};

let applyStyle = function (tab, property, value) {
  tab.style.setProperty(property,
                        value,
                        'important');  
};

let applyStyles = function (tab, url) {
  if (!url) {
    url = getURI(tab);
  }

  if (url in addonTabs && addonTabs[url] != null) {
    for (let item in addonTabs[url]) {
      applyStyle(tab, item, addonTabs[url][item]);
    }
  }
};

/* Simply adds the URL/style to the list,
 * and then just open a tab using the high-level
 * tab module.
 */
exports.open = function (options) {
  addTabToList(options);
  let tab = tabs.open(options);
  return tab;
};

/*
 * Track tabs opened and closed.
 * Once you have a tab open, get the browser for it
 * and figure out the location (which turns out to be the right one, not about:blank)
 */
tabs.on('open', function onOpen(tab) {
  let browser = getBrowserForTab(tab);

  tab.on('load', function onTabLoad(e) {
    if (typeof browser == 'undefined') {
        return;
    }
    // let's get the location of the document.
    applyStyles(tab, browser.contentDocument.location);
  }, true);
});


WindowTracker({
  onTrack: function onTrack(window) {
    if (!isXULBrowser(window) || windows(window).hideChromeForLocation)
      return;
    
    let { XULBrowserWindow } = window;
    let { hideChromeForLocation } = XULBrowserWindow;
    
    windows(window).hideChromeForLocation = hideChromeForLocation;

    // Augmenting the behavior of `hideChromeForLocation` method, as
    // suggested by https://developer.mozilla.org/en-US/docs/Hiding_browser_chrome
    XULBrowserWindow.hideChromeForLocation = function(url) {
      
        if (url in addonTabs)
          return true;

      return hideChromeForLocation.call(this, url);
    };
  },

  onUntrack: function onUntrack(window) {
    if (isXULBrowser(window))
      getTabs(window).filter(tabFilter).forEach(untrackTab.bind(null, window));

  }
});


function tabFilter(tab) {
  if (getURI(tab) in addonTabs) {
    return true;
  }

  return false;
}

function untrackTab(window, tab) {
  // Note: `onUntrack` will be called for all windows on add-on unloads,
  // so we want to clean them up from these URLs.
  let { hideChromeForLocation } = windows(window);

  if (hideChromeForLocation) {
    window.XULBrowserWindow.hideChromeForLocation = hideChromeForLocation;
    windows(window).hideChromeForLocation = null;
  }
  // had to remove closeTab call to prevent a TypeError: element is
  // undefined.
  //closeTab(tab);
}