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.

366 lines
11 KiB

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. 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. NOOPFUNC: function(){},
  176. UNHANDLED: 'vAPI.messaging.notHandled'
  177. };
  178. /******************************************************************************/
  179. vAPI.messaging.listen = function(listenerName, callback) {
  180. this.listeners[listenerName] = callback;
  181. };
  182. /******************************************************************************/
  183. vAPI.messaging.onPortMessage = function(request, port) {
  184. var callback = vAPI.messaging.NOOPFUNC;
  185. if ( request.requestId !== undefined ) {
  186. callback = function(response) {
  187. port.postMessage({
  188. requestId: request.requestId,
  189. portName: request.portName,
  190. msg: response !== undefined ? response : null
  191. });
  192. };
  193. }
  194. // Specific handler
  195. var r = vAPI.messaging.UNHANDLED;
  196. var listener = vAPI.messaging.listeners[request.portName];
  197. if ( typeof listener === 'function' ) {
  198. r = listener(request.msg, port.sender, callback);
  199. }
  200. if ( r !== vAPI.messaging.UNHANDLED ) {
  201. return;
  202. }
  203. // Default handler
  204. r = vAPI.messaging.defaultHandler(request.msg, port.sender, callback);
  205. if ( r !== vAPI.messaging.UNHANDLED ) {
  206. return;
  207. }
  208. console.error('µBlock> messaging > unknown request: %o', request);
  209. // Unhandled:
  210. // Need to callback anyways in case caller expected an answer, or
  211. // else there is a memory leak on caller's side
  212. callback();
  213. };
  214. /******************************************************************************/
  215. vAPI.messaging.onPortDisconnect = function(port) {
  216. port.onDisconnect.removeListener(vAPI.messaging.onPortDisconnect);
  217. port.onMessage.removeListener(vAPI.messaging.onPortMessage);
  218. delete vAPI.messaging.ports[port.name];
  219. };
  220. /******************************************************************************/
  221. vAPI.messaging.onPortConnect = function(port) {
  222. port.onDisconnect.addListener(vAPI.messaging.onPortDisconnect);
  223. port.onMessage.addListener(vAPI.messaging.onPortMessage);
  224. vAPI.messaging.ports[port.name] = port;
  225. };
  226. /******************************************************************************/
  227. vAPI.messaging.setup = function(defaultHandler) {
  228. // Already setup?
  229. if ( this.defaultHandler !== null ) {
  230. return;
  231. }
  232. if ( typeof defaultHandler !== 'function' ) {
  233. defaultHandler = function(){ return vAPI.messaging.UNHANDLED; };
  234. }
  235. this.defaultHandler = defaultHandler;
  236. chrome.runtime.onConnect.addListener(this.onPortConnect);
  237. };
  238. /******************************************************************************/
  239. vAPI.messaging.broadcast = function(message) {
  240. var messageWrapper = {
  241. broadcast: true,
  242. msg: message
  243. };
  244. for ( var portName in this.ports ) {
  245. if ( this.ports.hasOwnProperty(portName) === false ) {
  246. continue;
  247. }
  248. this.ports[portName].postMessage(messageWrapper);
  249. }
  250. };
  251. /******************************************************************************/
  252. vAPI.net = {
  253. registerListeners: function() {
  254. var listeners = [
  255. 'onBeforeRequest',
  256. 'onBeforeSendHeaders',
  257. 'onHeadersReceived'
  258. ];
  259. for (var i = 0; i < listeners.length; ++i) {
  260. chrome.webRequest[listeners[i]].addListener(
  261. this[listeners[i]].callback,
  262. {
  263. 'urls': this[listeners[i]].urls || ['<all_urls>'],
  264. 'types': this[listeners[i]].types || []
  265. },
  266. this[listeners[i]].extra
  267. );
  268. }
  269. }
  270. };
  271. /******************************************************************************/
  272. vAPI.contextMenu = {
  273. create: function(details, callback) {
  274. this.menuId = details.id;
  275. this.callback = callback;
  276. chrome.contextMenus.create(details);
  277. chrome.contextMenus.onClicked.addListener(this.callback);
  278. },
  279. remove: function() {
  280. chrome.contextMenus.onClicked.removeListener(this.callback);
  281. chrome.contextMenus.remove(this.menuId);
  282. }
  283. };
  284. /******************************************************************************/
  285. vAPI.lastError = function() {
  286. return chrome.runtime.lastError;
  287. };
  288. /******************************************************************************/
  289. })();
  290. /******************************************************************************/