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.

     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/. */
     5 const tabs = require('sdk/tabs');
     6 const widgets = require('sdk/widget');
     7 const data = require('sdk/self').data;
     8 const pageMod = require('sdk/page-mod');
     9 const panel = require('sdk/panel');
    11 const ICON_WIDTH = 16;
    13 function updateWidgetView(tab) {
    14   let widgetView = widget.getView(tab.window);
    15   if (!tab.libraries) {
    16     tab.libraries = [];
    17   }
    18   widgetView.port.emit("update", tab.libraries);
    19   widgetView.width = tab.libraries.length * ICON_WIDTH;
    20 }
    22 var widget = widgets.Widget({
    23   id: "library-detector",
    24   label: "Library Detector",
    25   contentURL: data.url("widget.html"),
    26   panel: panel.Panel({
    27     width: 240,
    28     height: 60,
    29     contentURL: data.url("panel.html")
    30   })
    31 });
    33 widget.port.on('setLibraryInfo', function(libraryInfo) {
    34   widget.panel.postMessage(libraryInfo);
    35 });
    37 pageMod.PageMod({
    38   include: "*",
    39   contentScriptWhen: 'end',
    40   contentScriptFile: (data.url('library-detector.js')),
    41   onAttach: function(worker) {
    42     worker.on('message', function(libraryList) {
    43       if (!worker.tab.libraries) {
    44         worker.tab.libraries = [];
    45       }
    46       libraryList.forEach(function(library) {
    47         if (worker.tab.libraries.indexOf(library) == -1) {
    48           worker.tab.libraries.push(library);
    49         }
    50       });
    51       if (worker.tab == tabs.activeTab) {
    52         updateWidgetView(worker.tab);
    53       }
    54     });
    55   }
    56 });
    58 tabs.on('activate', function(tab) {
    59   updateWidgetView(tab);
    60 });
    62 /*
    63 For change of location
    64 */
    65 tabs.on('ready', function(tab) {
    66   tab.libraries = [];
    67 });

mercurial