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.

371 lines
11 KiB

10 years ago
10 years ago
10 years ago
  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 targetURL = details.url;
  65. if ( typeof targetURL !== 'string' || targetURL === '' ) {
  66. return null;
  67. }
  68. // extension pages
  69. if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
  70. targetURL = vAPI.getURL(targetURL);
  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: targetURL,
  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 = targetURL.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 ) {
  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. var onScriptExecuted = function() {
  144. // Must check `lastError` or else this may happen in the console:
  145. // Unchecked runtime.lastError while running tabs.executeScript: The tab was closed.
  146. if ( chrome.runtime.lastError ) {
  147. }
  148. if ( typeof callback === 'function' ) {
  149. callback();
  150. }
  151. };
  152. if ( tabId ) {
  153. chrome.tabs.executeScript(tabId, details, onScriptExecuted);
  154. } else {
  155. chrome.tabs.executeScript(details, onScriptExecuted);
  156. }
  157. };
  158. /******************************************************************************/
  159. // Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
  160. // https://github.com/gorhill/uBlock/issues/19
  161. // https://github.com/gorhill/uBlock/issues/207
  162. // Since we may be called asynchronously, the tab id may not exist
  163. // anymore, so this ensures it does still exist.
  164. vAPI.setIcon = function(tabId, img, badge) {
  165. var onIconReady = function() {
  166. if ( vAPI.lastError() ) {
  167. return;
  168. }
  169. chrome.browserAction.setBadgeText({ tabId: tabId, text: badge });
  170. if ( badge !== '' ) {
  171. chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, color: '#666' });
  172. }
  173. };
  174. chrome.browserAction.setIcon({ tabId: tabId, path: img }, onIconReady);
  175. };
  176. /******************************************************************************/
  177. vAPI.messaging = {
  178. ports: {},
  179. listeners: {},
  180. defaultHandler: null,
  181. NOOPFUNC: function(){},
  182. UNHANDLED: 'vAPI.messaging.notHandled'
  183. };
  184. /******************************************************************************/
  185. vAPI.messaging.listen = function(listenerName, callback) {
  186. this.listeners[listenerName] = callback;
  187. };
  188. /******************************************************************************/
  189. vAPI.messaging.onPortMessage = function(request, port) {
  190. var callback = vAPI.messaging.NOOPFUNC;
  191. if ( request.requestId !== undefined ) {
  192. callback = function(response) {
  193. port.postMessage({
  194. requestId: request.requestId,
  195. portName: request.portName,
  196. msg: response !== undefined ? response : null
  197. });
  198. };
  199. }
  200. // Specific handler
  201. var r = vAPI.messaging.UNHANDLED;
  202. var listener = vAPI.messaging.listeners[request.portName];
  203. if ( typeof listener === 'function' ) {
  204. r = listener(request.msg, port.sender, callback);
  205. }
  206. if ( r !== vAPI.messaging.UNHANDLED ) {
  207. return;
  208. }
  209. // Default handler
  210. r = vAPI.messaging.defaultHandler(request.msg, port.sender, callback);
  211. if ( r !== vAPI.messaging.UNHANDLED ) {
  212. return;
  213. }
  214. console.error('µBlock> messaging > unknown request: %o', request);
  215. // Unhandled:
  216. // Need to callback anyways in case caller expected an answer, or
  217. // else there is a memory leak on caller's side
  218. callback();
  219. };
  220. /******************************************************************************/
  221. vAPI.messaging.onPortDisconnect = function(port) {
  222. port.onDisconnect.removeListener(vAPI.messaging.onPortDisconnect);
  223. port.onMessage.removeListener(vAPI.messaging.onPortMessage);
  224. delete vAPI.messaging.ports[port.name];
  225. };
  226. /******************************************************************************/
  227. vAPI.messaging.onPortConnect = function(port) {
  228. port.onDisconnect.addListener(vAPI.messaging.onPortDisconnect);
  229. port.onMessage.addListener(vAPI.messaging.onPortMessage);
  230. vAPI.messaging.ports[port.name] = port;
  231. };
  232. /******************************************************************************/
  233. vAPI.messaging.setup = function(defaultHandler) {
  234. // Already setup?
  235. if ( this.defaultHandler !== null ) {
  236. return;
  237. }
  238. if ( typeof defaultHandler !== 'function' ) {
  239. defaultHandler = function(){ return vAPI.messaging.UNHANDLED; };
  240. }
  241. this.defaultHandler = defaultHandler;
  242. chrome.runtime.onConnect.addListener(this.onPortConnect);
  243. };
  244. /******************************************************************************/
  245. vAPI.messaging.broadcast = function(message) {
  246. var messageWrapper = {
  247. broadcast: true,
  248. msg: message
  249. };
  250. for ( var portName in this.ports ) {
  251. if ( this.ports.hasOwnProperty(portName) === false ) {
  252. continue;
  253. }
  254. this.ports[portName].postMessage(messageWrapper);
  255. }
  256. };
  257. /******************************************************************************/
  258. vAPI.net = {
  259. registerListeners: function() {
  260. var listeners = [
  261. 'onBeforeRequest',
  262. 'onBeforeSendHeaders',
  263. 'onHeadersReceived'
  264. ];
  265. for (var i = 0; i < listeners.length; ++i) {
  266. chrome.webRequest[listeners[i]].addListener(
  267. this[listeners[i]].callback,
  268. {
  269. 'urls': this[listeners[i]].urls || ['<all_urls>'],
  270. 'types': this[listeners[i]].types || []
  271. },
  272. this[listeners[i]].extra
  273. );
  274. }
  275. }
  276. };
  277. /******************************************************************************/
  278. vAPI.contextMenu = {
  279. create: function(details, callback) {
  280. this.menuId = details.id;
  281. this.callback = callback;
  282. chrome.contextMenus.create(details);
  283. chrome.contextMenus.onClicked.addListener(this.callback);
  284. },
  285. remove: function() {
  286. chrome.contextMenus.onClicked.removeListener(this.callback);
  287. chrome.contextMenus.remove(this.menuId);
  288. }
  289. };
  290. /******************************************************************************/
  291. vAPI.lastError = function() {
  292. return chrome.runtime.lastError;
  293. };
  294. /******************************************************************************/
  295. })();
  296. /******************************************************************************/