ui.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global singlefile, navigator */
  21. singlefile.ui = (() => {
  22. const browser = this.browser || this.chrome;
  23. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_19.png";
  24. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_19_wait";
  25. const DEFAULT_TITLE = "Process this page with SingleFile";
  26. const DEFAULT_COLOR = [2, 147, 20, 255];
  27. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  28. const RUNNING_IN_EDGE = navigator.userAgent.includes("Edge");
  29. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  30. const MENU_ID_SAVE_PAGE = "save-page";
  31. const MENU_ID_SAVE_SELECTED = "save-selected";
  32. const tabs = {};
  33. const badgeTabs = {};
  34. let badgeRefreshPending = [];
  35. browser.runtime.onInstalled.addListener(refreshContextMenu);
  36. browser.contextMenus.onClicked.addListener((event, tab) => {
  37. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  38. processTab(tab);
  39. }
  40. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  41. processTab(tab, { selected: true });
  42. }
  43. });
  44. browser.browserAction.onClicked.addListener(tab => {
  45. if (isAllowedURL(tab.url)) {
  46. browser.tabs.query({ currentWindow: true, highlighted: true }, tabs => tabs.forEach(processTab));
  47. }
  48. });
  49. browser.tabs.onActivated.addListener(activeInfo => browser.tabs.get(activeInfo.tabId, tab => onActive(tab.id, isAllowedURL(tab.url))));
  50. browser.tabs.onCreated.addListener(tab => onActive(tab.id, isAllowedURL(tab.url)));
  51. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onActive(tab.id, isAllowedURL(tab.url)));
  52. browser.tabs.onRemoved.addListener(tabId => onRemoved(tabId));
  53. browser.runtime.onMessage.addListener((request, sender) => {
  54. if (request.processStart || request.processProgress) {
  55. onProgress(sender.tab.id, request.index, request.maxIndex);
  56. }
  57. if (request.processEnd) {
  58. onEnd(sender.tab.id);
  59. }
  60. if (request.processError) {
  61. onError(sender.tab.id);
  62. }
  63. return false;
  64. });
  65. return { update: refreshContextMenu };
  66. function refreshContextMenu() {
  67. const contextMenuEnabled = singlefile.config.get().contextMenuEnabled;
  68. if (contextMenuEnabled) {
  69. browser.contextMenus.create({
  70. id: MENU_ID_SAVE_PAGE,
  71. contexts: ["page"],
  72. title: "Save page with SingleFile"
  73. });
  74. browser.contextMenus.create({
  75. id: MENU_ID_SAVE_SELECTED,
  76. contexts: ["selection"],
  77. title: "Save selection with SingleFile"
  78. });
  79. } else {
  80. browser.contextMenus.removeAll();
  81. }
  82. }
  83. function processTab(tab) {
  84. const tabId = tab.id;
  85. singlefile.core.processTab(tab);
  86. tabs[tabId] = {
  87. id: tabId,
  88. text: "...",
  89. color: DEFAULT_COLOR,
  90. title: "initializing...",
  91. path: DEFAULT_ICON_PATH,
  92. progress: -1,
  93. barProgress: -1
  94. };
  95. refreshBadge(tabId);
  96. }
  97. function onError(tabId) {
  98. const tabData = tabs[tabId];
  99. tabData.text = "ERR";
  100. tabData.color = [229, 4, 12, 255];
  101. tabData.title = DEFAULT_TITLE;
  102. tabData.path = DEFAULT_ICON_PATH;
  103. tabData.progress = -1;
  104. tabData.barProgress = -1;
  105. refreshBadge(tabId);
  106. }
  107. function onEnd(tabId) {
  108. const tabData = tabs[tabId];
  109. tabData.text = "OK";
  110. tabData.color = [4, 229, 36, 255];
  111. tabData.title = DEFAULT_TITLE;
  112. tabData.path = DEFAULT_ICON_PATH;
  113. tabData.progress = -1;
  114. tabData.barProgress = -1;
  115. refreshBadge(tabId);
  116. }
  117. function onProgress(tabId, index, maxIndex) {
  118. const tabData = tabs[tabId];
  119. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  120. if (tabData.progress != progress) {
  121. tabData.progress = progress;
  122. tabData.text = "";
  123. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  124. tabData.color = [4, 229, 36, 255];
  125. const barProgress = Math.floor((index / maxIndex) * 15);
  126. if (tabData.barProgress != barProgress) {
  127. tabData.barProgress = barProgress;
  128. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  129. }
  130. refreshBadge(tabId);
  131. }
  132. }
  133. function onRemoved(tabId) {
  134. delete tabs[tabId];
  135. }
  136. function onActive(tabId, isActive) {
  137. if (isActive) {
  138. browser.browserAction.enable(tabId);
  139. } else {
  140. browser.browserAction.disable(tabId);
  141. }
  142. }
  143. function isAllowedURL(url) {
  144. return (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  145. }
  146. async function refreshBadge(tabId) {
  147. await Promise.all(badgeRefreshPending);
  148. const promise = refreshBadgeAsync(tabId);
  149. badgeRefreshPending.push(promise);
  150. await promise;
  151. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  152. }
  153. async function refreshBadgeAsync(tabId) {
  154. for (let property of BADGE_PROPERTIES) {
  155. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  156. }
  157. }
  158. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  159. const tabData = tabs[tabId];
  160. const value = tabData[property];
  161. if (!badgeTabs[tabId]) {
  162. badgeTabs[tabId] = {};
  163. }
  164. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  165. const browserActionParameter = { tabId };
  166. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  167. return new Promise(resolve => {
  168. if (RUNNING_IN_EDGE) {
  169. browser.browserAction[browserActionMethod](browserActionParameter);
  170. resolve();
  171. } else {
  172. browser.browserAction[browserActionMethod](browserActionParameter, resolve);
  173. }
  174. });
  175. }
  176. }
  177. })();