You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
11 KiB

  1. /*******************************************************************************
  2. µBlock - a Chromium browser extension to block requests.
  3. Copyright (C) 2014 The µBlock authors
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uBlock
  15. */
  16. /* global self */
  17. // For background page
  18. /******************************************************************************/
  19. (function() {
  20. 'use strict';
  21. /******************************************************************************/
  22. self.vAPI = self.vAPI || {};
  23. var vAPI = self.vAPI;
  24. var chrome = self.chrome;
  25. vAPI.chrome = true;
  26. /******************************************************************************/
  27. vAPI.storage = chrome.storage.local;
  28. /******************************************************************************/
  29. vAPI.tabs = {};
  30. /******************************************************************************/
  31. vAPI.tabs.registerListeners = function() {
  32. if ( typeof this.onNavigation === 'function' ) {
  33. chrome.webNavigation.onCommitted.addListener(this.onNavigation);
  34. }
  35. if ( typeof this.onUpdated === 'function' ) {
  36. chrome.tabs.onUpdated.addListener(this.onUpdated);
  37. }
  38. if ( typeof this.onClosed === 'function' ) {
  39. chrome.tabs.onRemoved.addListener(this.onClosed);
  40. }
  41. if ( typeof this.onPopup === 'function' ) {
  42. chrome.webNavigation.onCreatedNavigationTarget.addListener(this.onPopup);
  43. }
  44. };
  45. /******************************************************************************/
  46. vAPI.tabs.get = function(tabId, callback) {
  47. if ( tabId !== null ) {
  48. chrome.tabs.get(tabId, callback);
  49. return;
  50. }
  51. var onTabReceived = function(tabs) {
  52. callback(tabs[0]);
  53. };
  54. chrome.tabs.query({ active: true, currentWindow: true }, onTabReceived);
  55. };
  56. /******************************************************************************/
  57. // properties of the details object:
  58. // url: 'URL', // the address that will be opened
  59. // tabId: 1, // the tab is used if set, instead of creating a new one
  60. // index: -1, // undefined: end of the list, -1: following tab, or after index
  61. // active: false, // opens the tab in background - true and undefined: foreground
  62. // select: true // if a tab is already opened with that url, then select it instead of opening a new one
  63. vAPI.tabs.open = function(details) {
  64. var url = details.url;
  65. if ( typeof url !== 'string' || url === '' ) {
  66. return null;
  67. }
  68. // extension pages
  69. if ( /^[\w-]{2,}:/.test(url) !== true ) {
  70. url = vAPI.getURL(url);
  71. }
  72. // dealing with Chrome's asynchronous API
  73. var wrapper = function() {
  74. if ( details.active === undefined ) {
  75. details.active = true;
  76. }
  77. var subWrapper = function() {
  78. var _details = {
  79. url: details.url,
  80. active: !!details.active
  81. };
  82. if ( details.tabId ) {
  83. // update doesn't accept index, must use move
  84. chrome.tabs.update(details.tabId, _details, function(tab) {
  85. // if the tab doesn't exist
  86. if ( vAPI.lastError() ) {
  87. chrome.tabs.create(_details);
  88. } else if ( details.index !== undefined ) {
  89. chrome.tabs.move(tab.id, {index: details.index});
  90. }
  91. });
  92. } else {
  93. if ( details.index !== undefined ) {
  94. _details.index = details.index;
  95. }
  96. chrome.tabs.create(_details);
  97. }
  98. };
  99. if ( details.index === -1 ) {
  100. vAPI.tabs.get(null, function(tab) {
  101. if ( tab ) {
  102. details.index = tab.index + 1;
  103. } else {
  104. delete details.index;
  105. }
  106. subWrapper();
  107. });
  108. }
  109. else {
  110. subWrapper();
  111. }
  112. };
  113. if ( details.select ) {
  114. chrome.tabs.query({ currentWindow: true }, function(tabs) {
  115. var url = details.url.replace(rgxHash, '');
  116. // this is questionable
  117. var rgxHash = /#.*/;
  118. var selected = tabs.some(function(tab) {
  119. if ( tab.url.replace(rgxHash, '') === url ) {
  120. chrome.tabs.update(tab.id, { active: true });
  121. return true;
  122. }
  123. });
  124. if ( selected.length === 0 ) {
  125. wrapper();
  126. }
  127. });
  128. }
  129. else {
  130. wrapper();
  131. }
  132. };
  133. /******************************************************************************/
  134. vAPI.tabs.remove = function(tabId) {
  135. var onTabRemoved = function() {
  136. if ( vAPI.lastError() ) {
  137. }
  138. };
  139. chrome.tabs.remove(tabId, onTabRemoved);
  140. };
  141. /******************************************************************************/
  142. vAPI.tabs.injectScript = function(tabId, details, callback) {
  143. if ( typeof callback !== 'function' ) {
  144. callback = function(){};
  145. }
  146. if ( tabId ) {
  147. chrome.tabs.executeScript(tabId, details, callback);
  148. } else {
  149. chrome.tabs.executeScript(details, callback);
  150. }
  151. };
  152. /******************************************************************************/
  153. // Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
  154. // https://github.com/gorhill/uBlock/issues/19
  155. // https://github.com/gorhill/uBlock/issues/207
  156. // Since we may be called asynchronously, the tab id may not exist
  157. // anymore, so this ensures it does still exist.
  158. vAPI.setIcon = function(tabId, img, badge) {
  159. var onIconReady = function() {
  160. if ( vAPI.lastError() ) {
  161. return;
  162. }
  163. chrome.browserAction.setBadgeText({ tabId: tabId, text: badge });
  164. if ( badge !== '' ) {
  165. chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, color: '#666' });
  166. }
  167. };
  168. chrome.browserAction.setIcon({ tabId: tabId, path: img }, onIconReady);
  169. };
  170. /******************************************************************************/
  171. vAPI.messaging = {
  172. ports: {},
  173. listeners: {},
  174. defaultHandler: null,
  175. UNHANDLED: 'vAPI.messaging.notHandled'
  176. };
  177. /******************************************************************************/
  178. vAPI.messaging.listen = function(listenerName, callback) {
  179. this.listeners[listenerName] = callback;
  180. };
  181. /******************************************************************************/
  182. vAPI.messaging.onConnect = function(port) {
  183. var onMessage = function(request) {
  184. var callback = function(response) {
  185. if ( vAPI.lastError() || response === undefined ) {
  186. return;
  187. }
  188. if ( request.requestId ) {
  189. port.postMessage({
  190. requestId: request.requestId,
  191. portName: request.portName,
  192. msg: response
  193. });
  194. }
  195. };
  196. // Specific handler
  197. var r;
  198. var listener = vAPI.messaging.listeners[request.portName];
  199. if ( typeof listener === 'function' ) {
  200. r = listener(request.msg, port.sender, callback);
  201. }
  202. if ( r !== vAPI.messaging.UNHANDLED ) {
  203. return;
  204. }
  205. // Default handler
  206. r = vAPI.messaging.defaultHandler(request.msg, port.sender, callback);
  207. if ( r !== vAPI.messaging.UNHANDLED ) {
  208. return;
  209. }
  210. console.error('µBlock> messaging > unknown request: %o', request);
  211. };
  212. var onDisconnect = function(port) {
  213. port.onDisconnect.removeListener(onDisconnect);
  214. port.onMessage.removeListener(onMessage);
  215. delete vAPI.messaging.ports[port.name];
  216. };
  217. port.onDisconnect.addListener(onDisconnect);
  218. port.onMessage.addListener(onMessage);
  219. vAPI.messaging.ports[port.name] = port;
  220. };
  221. /******************************************************************************/
  222. vAPI.messaging.setup = function(defaultHandler) {
  223. // Already setup?
  224. if ( this.defaultHandler !== null ) {
  225. return;
  226. }
  227. if ( typeof defaultHandler !== 'function' ) {
  228. defaultHandler = function(){ return null; };
  229. };
  230. this.defaultHandler = defaultHandler;
  231. chrome.runtime.onConnect.addListener(this.onConnect);
  232. };
  233. /******************************************************************************/
  234. vAPI.messaging.broadcast = function(message) {
  235. var messageWrapper = {
  236. broadcast: true,
  237. msg: message
  238. };
  239. for ( var portName in this.ports ) {
  240. if ( this.ports.hasOwnProperty(portName) === false ) {
  241. continue;
  242. }
  243. this.ports[portName].postMessage(messageWrapper);
  244. }
  245. };
  246. /******************************************************************************/
  247. vAPI.net = {
  248. registerListeners: function() {
  249. var listeners = [
  250. 'onBeforeRequest',
  251. 'onBeforeSendHeaders',
  252. 'onHeadersReceived'
  253. ];
  254. for (var i = 0; i < listeners.length; ++i) {
  255. chrome.webRequest[listeners[i]].addListener(
  256. this[listeners[i]].callback,
  257. {
  258. 'urls': this[listeners[i]].urls || ['<all_urls>'],
  259. 'types': this[listeners[i]].types || []
  260. },
  261. this[listeners[i]].extra
  262. );
  263. }
  264. }
  265. };
  266. /******************************************************************************/
  267. vAPI.contextMenu = {
  268. create: function(details, callback) {
  269. this.menuId = details.id;
  270. this.callback = callback;
  271. chrome.contextMenus.create(details);
  272. chrome.contextMenus.onClicked.addListener(this.callback);
  273. },
  274. remove: function() {
  275. chrome.contextMenus.onClicked.removeListener(this.callback);
  276. chrome.contextMenus.remove(this.menuId);
  277. }
  278. };
  279. /******************************************************************************/
  280. vAPI.lastError = function() {
  281. return chrome.runtime.lastError;
  282. };
  283. /******************************************************************************/
  284. })();
  285. /******************************************************************************/