main.js 1.7 KB

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