You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
10 KiB
JavaScript

/* jshint ignore:start */
const SDK = {
data: require("sdk/self").data,
Ci: require('chrome').Ci,
runtime: require('sdk/system/runtime'),
winutils: require('sdk/window/utils'),
storage: require('sdk/simple-storage').storage,
tabs: require("sdk/tabs"),
// tabshelpers: require('sdk/tabs/helpers'),
tabsutils: require("sdk/tabs/utils"),
unload: require('sdk/system/unload')
};
const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var browserURL = "chrome://browser/content/browser.xul";
var optionsPage = SDK.data.url("options/options.html");
var options = {}, button_id = 'infortst_btn', item_id = "informst_button", badge_id = "informst_badge";
var listeners_after = [], listeners_change = [], buttonedWindows = [];
function openOptions() {
allTabs = SDK.tabsutils.getTabs();
isOpened = false;
for (tab in allTabs)
if (SDK.tabsutils.getURI(allTabs[tab]) == optionsPage) {
isOpened = true;
tabOpened = allTabs[tab];
}
if (isOpened) {
if (tabOpened) SDK.tabsutils.activateTab(tabOpened, SDK.tabsutils.getOwnerWindow(tabOpened));
} else {
SDK.tabs.open(optionsPage);
}
}
function prefs(key, value) {
if (typeof value != 'undefined') {
SDK.storage[key] = value;
} else {
return SDK.storage[key];
}
};
function create(window, opts) {
options = opts;
let doc = window.document;
let $ = function(id) doc.getElementById(id);
var util = window.QueryInterface(SDK.Ci.nsIInterfaceRequestor).getInterface(SDK.Ci.nsIDOMWindowUtils);
var windowID = util.outerWindowID;
if (buttonedWindows.indexOf(windowID) == -1)
buttonedWindows.push(windowID);
else
return;
let tbb = doc.createElementNS(NS_XUL, "toolbarbutton");
let tbbContainer = doc.createElementNS(NS_XUL, "toolbaritem");
let tbbStack = doc.createElementNS(NS_XUL, "stack");
let tbbBadge = doc.createElementNS(NS_XUL, "label");
tbbBadge.setAttribute('id', badge_id);
var binding =
'' +
'' +
'' +
'+ options.stylesheet + '"/>' +
'' +
'' +
'';
tbbBadge.style.MozBinding = 'url("data:text/xml;charset=utf-8,' + doc.defaultView.encodeURIComponent(binding) + '")';
tbbBadge.style.pointerEvents = 'none';
tbbContainer.setAttribute("id", button_id);
tbbContainer.setAttribute("label", options.label);
tbbContainer.setAttribute('tooltiptext', options.tooltiptext);
tbb.setAttribute("id", item_id);
tbb.setAttribute("type", "button");
tbb.setAttribute("orient", "vertical");
tbb.setAttribute("label", options.label);
tbb.setAttribute("class", "toolbarbutton-1");
tbb.addEventListener("command", function() {
option.onCommand({});
options.panel.show({ focus: false }, tbbContainer);
}, true);
tbb.addEventListener("click", function(e) {
if (e.button == 0)
options.panel.show({ focus: false }, tbbContainer);
else if (e.button == 1)
openOptions();
}, true);
positionBadge(tbbBadge);
tbbStack.appendChild(tbb);
tbbStack.appendChild(tbbBadge);
tbbContainer.appendChild(tbbStack);
try {
var inPalette = ($("navigator-toolbox") || $("mail-toolbox")).palette.getElementsByAttribute('id', button_id);
if (inPalette.length == 0)
($("navigator-toolbox") || $("mail-toolbox")).palette.appendChild(tbbContainer);
} catch (e) { }
var toolbar = $('nav-bar');
if (prefs('badgeLocation')) toolbar = $(prefs('badgeLocation'));
if (toolbar === 'hidden') return;
if (!toolbar) toolbar = toolbarbuttonExists(doc, button_id);
if (toolbar) {
let b4;
if (prefs('badgeLocationInsertBefore'))
b4 = $(prefs('badgeLocationInsertBefore'));
if (!b4) {
let currentset = toolbar.getAttribute("currentset").split(",");
let i = currentset.indexOf(button_id) + 1;
if (i > 0) {
let len = currentset.length;
for (; i < len; i++) {
b4 = $(currentset[i]);
if (b4) break;
}
}
}
toolbar.insertItem && toolbar.insertItem(button_id, b4, null, false);
toolbar.setAttribute("currentset", toolbar.currentSet);
try {
var keyset = doc.createElement('keyset'), keyelem = doc.createElement('key');
keyset.id = 'informst_keyset';
keyelem.setAttribute('id', 'informst_finder_key');
// keyelem.setAttribute('oncommand', "void(0);");
keyelem.addEventListener("command", function() { tbb.click(); }, true);
keyelem.setAttribute('key', 'b');
keyelem.setAttribute('modifiers', 'alt control');
keyset.appendChild(keyelem);
$('main-window').appendChild(keyset);
} catch (err) {
}
doc.persist(toolbar.id, "currentset");
}
var saveNodeInfo = function(e) {
toolbarID = tbbContainer.parentNode.getAttribute("id") || "";
insertbefore = (tbbContainer.nextSibling || "") && tbbContainer.nextSibling.getAttribute("id").replace(/^wrapper-/i, "");
var doc = e.target.ownerDocument, badge = $(button_id);
if (!!badge) {
prefs('badgeLocation', badge.parentElement.id);
if (badge.nextSibling)
prefs('badgeLocationInsertBefore', badge.nextSibling.id);
else
prefs('badgeLocationInsertBefore', false);
} else {
prefs('badgeLocationInsertBefore', false);
prefs('badgeLocation', 'hidden');
}
options.afterCustomization({});
};
window.addEventListener("aftercustomization", saveNodeInfo, false);
listeners_after.push(saveNodeInfo);
var updateNodeImage = function(e) {
var isSmallIcon = (toolbarbuttonExists(window.document, button_id) !== false);
if (isSmallIcon)
tbb.setAttribute("image", options.image);
else
tbb.setAttribute("image", options.imageLarge);
}
updateNodeImage();
window.addEventListener("customizationchange", updateNodeImage, false);
listeners_change.push(updateNodeImage);
window.addEventListener('close', function() {
try {
tbbContainer.parentNode.removeChild(tbbContainer);
window.removeEventListener("aftercustomization", saveNodeInfo, false);
window.removeEventListener("customizationchange", updateNodeImage, false);
} catch (e) {
}
});
}
function toolbarbuttonExists(doc, id) {
var toolbars = doc.getElementsByTagNameNS(NS_XUL, "toolbar");
for (var i = 0; i < toolbars.length - 1; i++) {
let currentset = toolbars[i].getAttribute("currentset").split(",");
let len = currentset.length;
for (var j = 0; j < len; j++)
if (currentset[j] == id) return toolbars[i];
}
return false;
}
function positionBadge(buttonBadge) {
var right, bottom;
switch (SDK.runtime.OS) {
case 'Darwin':
right = '-5';
bottom = '-5';
break;
case 'WINNT':
right = '-1';
bottom = '3';
break;
case 'Linux':
right = '-3';
bottom = '0';
break;
default:
right = '-5';
bottom = '-5';
}
buttonBadge.setAttribute('right', right);
buttonBadge.setAttribute('bottom', bottom);
}
exports.moveTo = function(toolbarID) {
for (var window in SDK.winutils.windows(null, { includePrivate: true })) {
window = SDK.winutils.windows(null, { includePrivate: true })[window];
if (browserURL != window.location) return;
let doc = window.document;
let $ = function (id) doc.getElementById(id);
if ($(button_id)) return;
var tb = $(toolbarID), b4;
if (prefs('badgeLocationInsertBefore'))
b4 = $(prefs('badgeLocationInsertBefore'));
if (tb) {
tb.insertItem(button_id, b4, null, false);
tb.setAttribute("currentset", tb.currentSet);
doc.persist(tb.id, "currentset");
prefs('badgeLocation', toolbarID);
}
}
}
exports.setIcon = function(pathToImage) {
for (var window in SDK.winutils.windows(null, { includePrivate: true })) {
window = SDK.winutils.windows(null, { includePrivate: true })[window];
if (browserURL != window.location) continue;
// if (tab_id != SDK.tabshelpers.getTabForWindow(window.content).id) continue;
var button = window.document.getElementById(item_id);
if (!button) continue;
button.setAttribute('image', pathToImage);
}
}
exports.setLabel = function(newLabel) {
for (var window in SDK.winutils.windows(null, { includePrivate: true })) {
window = SDK.winutils.windows(null, { includePrivate: true })[window];
if (browserURL != window.location) continue;
// if (tab_id != SDK.tabshelpers.getTabForWindow(window.content).id) continue;
var buttonContainer = window.document.getElementById(button_id);
if (!buttonContainer) continue;
buttonContainer.setAttribute('tooltiptext', newLabel);
}
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
exports.setBadge = function (value, fcolor, bcolor) {
for (var window in SDK.winutils.windows(null, { includePrivate: true })) {
window = SDK.winutils.windows(null, { includePrivate: true })[window];
if (browserURL != window.location) continue;
// if (tab_id != SDK.tabshelpers.getTabForWindow(window.content).id) continue;
var badge = window.document.getElementById(badge_id);
if (!badge) continue;
badge.style.color = fcolor;
var bcolor_rgb = hexToRgb(bcolor);
badge.style.backgroundColor = 'rgba(' + bcolor_rgb.r + ', ' + bcolor_rgb.g + ', ' + bcolor_rgb.b + ', 0.7)';;
if (value || value == '0') {
badge.style.visibility = 'visible';
badge.style.display = '';
} else {
badge.style.visibility = 'hidden';
badge.style.display = 'none';
}
badge.setAttribute('value', value);
}
}
exports.create = create;
function remove() {
for (var window in SDK.winutils.windows(null, { includePrivate: true })) {
window = SDK.winutils.windows(null, { includePrivate: true })[window];
let doc = window.document;
let $ = function(id) doc.getElementById(id);
try {
var container = $(button_id);
if (!container) continue;
container.parentNode.removeChild(container);
var inPalette = ($("navigator-toolbox") || $("mail-toolbox")).palette.getElementsByAttribute( 'id', button_id );
if (inPalette.length == 1)
($("navigator-toolbox") || $("mail-toolbox")).palette.removeChild(inPalette[0]);
for (var i = 0; i < listeners_after.length; i++)
window.removeEventListener("aftercustomization", listeners_after[i], false);
for (var i = 0; i < listeners_change.length; i++)
window.removeEventListener("customizationchange", listeners_change[i], false);
} catch (e) {
console.log(e.toString());
}
}
listeners_after = [];
listeners_change = [];
}
exports.remove = remove;
SDK.unload.when(remove);