addon-sdk/source/examples/library-detector/lib/main.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const tabs = require('sdk/tabs');
michael@0 6 const widgets = require('sdk/widget');
michael@0 7 const data = require('sdk/self').data;
michael@0 8 const pageMod = require('sdk/page-mod');
michael@0 9 const panel = require('sdk/panel');
michael@0 10
michael@0 11 const ICON_WIDTH = 16;
michael@0 12
michael@0 13 function updateWidgetView(tab) {
michael@0 14 let widgetView = widget.getView(tab.window);
michael@0 15 if (!tab.libraries) {
michael@0 16 tab.libraries = [];
michael@0 17 }
michael@0 18 widgetView.port.emit("update", tab.libraries);
michael@0 19 widgetView.width = tab.libraries.length * ICON_WIDTH;
michael@0 20 }
michael@0 21
michael@0 22 var widget = widgets.Widget({
michael@0 23 id: "library-detector",
michael@0 24 label: "Library Detector",
michael@0 25 contentURL: data.url("widget.html"),
michael@0 26 panel: panel.Panel({
michael@0 27 width: 240,
michael@0 28 height: 60,
michael@0 29 contentURL: data.url("panel.html")
michael@0 30 })
michael@0 31 });
michael@0 32
michael@0 33 widget.port.on('setLibraryInfo', function(libraryInfo) {
michael@0 34 widget.panel.postMessage(libraryInfo);
michael@0 35 });
michael@0 36
michael@0 37 pageMod.PageMod({
michael@0 38 include: "*",
michael@0 39 contentScriptWhen: 'end',
michael@0 40 contentScriptFile: (data.url('library-detector.js')),
michael@0 41 onAttach: function(worker) {
michael@0 42 worker.on('message', function(libraryList) {
michael@0 43 if (!worker.tab.libraries) {
michael@0 44 worker.tab.libraries = [];
michael@0 45 }
michael@0 46 libraryList.forEach(function(library) {
michael@0 47 if (worker.tab.libraries.indexOf(library) == -1) {
michael@0 48 worker.tab.libraries.push(library);
michael@0 49 }
michael@0 50 });
michael@0 51 if (worker.tab == tabs.activeTab) {
michael@0 52 updateWidgetView(worker.tab);
michael@0 53 }
michael@0 54 });
michael@0 55 }
michael@0 56 });
michael@0 57
michael@0 58 tabs.on('activate', function(tab) {
michael@0 59 updateWidgetView(tab);
michael@0 60 });
michael@0 61
michael@0 62 /*
michael@0 63 For change of location
michael@0 64 */
michael@0 65 tabs.on('ready', function(tab) {
michael@0 66 tab.libraries = [];
michael@0 67 });

mercurial