1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/examples/library-detector/lib/main.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,67 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const tabs = require('sdk/tabs'); 1.9 +const widgets = require('sdk/widget'); 1.10 +const data = require('sdk/self').data; 1.11 +const pageMod = require('sdk/page-mod'); 1.12 +const panel = require('sdk/panel'); 1.13 + 1.14 +const ICON_WIDTH = 16; 1.15 + 1.16 +function updateWidgetView(tab) { 1.17 + let widgetView = widget.getView(tab.window); 1.18 + if (!tab.libraries) { 1.19 + tab.libraries = []; 1.20 + } 1.21 + widgetView.port.emit("update", tab.libraries); 1.22 + widgetView.width = tab.libraries.length * ICON_WIDTH; 1.23 +} 1.24 + 1.25 +var widget = widgets.Widget({ 1.26 + id: "library-detector", 1.27 + label: "Library Detector", 1.28 + contentURL: data.url("widget.html"), 1.29 + panel: panel.Panel({ 1.30 + width: 240, 1.31 + height: 60, 1.32 + contentURL: data.url("panel.html") 1.33 + }) 1.34 +}); 1.35 + 1.36 +widget.port.on('setLibraryInfo', function(libraryInfo) { 1.37 + widget.panel.postMessage(libraryInfo); 1.38 +}); 1.39 + 1.40 +pageMod.PageMod({ 1.41 + include: "*", 1.42 + contentScriptWhen: 'end', 1.43 + contentScriptFile: (data.url('library-detector.js')), 1.44 + onAttach: function(worker) { 1.45 + worker.on('message', function(libraryList) { 1.46 + if (!worker.tab.libraries) { 1.47 + worker.tab.libraries = []; 1.48 + } 1.49 + libraryList.forEach(function(library) { 1.50 + if (worker.tab.libraries.indexOf(library) == -1) { 1.51 + worker.tab.libraries.push(library); 1.52 + } 1.53 + }); 1.54 + if (worker.tab == tabs.activeTab) { 1.55 + updateWidgetView(worker.tab); 1.56 + } 1.57 + }); 1.58 + } 1.59 +}); 1.60 + 1.61 +tabs.on('activate', function(tab) { 1.62 + updateWidgetView(tab); 1.63 +}); 1.64 + 1.65 +/* 1.66 +For change of location 1.67 +*/ 1.68 +tabs.on('ready', function(tab) { 1.69 + tab.libraries = []; 1.70 +}); 1.71 \ No newline at end of file