LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 11 matching lines...) Expand all Loading... |
22 { | 22 { |
23 let nonEmptyPageMaps = new Set(); | 23 let nonEmptyPageMaps = new Set(); |
24 | 24 |
25 let PageMap = ext.PageMap = function() | 25 let PageMap = ext.PageMap = function() |
26 { | 26 { |
27 defineNamespace(this, internal); | 27 defineNamespace(this, internal); |
28 | 28 |
29 this[internal].map = new Map(); | 29 this[internal].map = new Map(); |
30 }; | 30 }; |
31 PageMap.prototype = { | 31 PageMap.prototype = { |
| 32 [internal]: { |
| 33 delete(pageId) |
| 34 { |
| 35 this[internal].map.delete(pageId); |
| 36 |
| 37 if (this[internal].map.size == 0) |
| 38 nonEmptyPageMaps.delete(this); |
| 39 } |
| 40 }, |
32 keys() | 41 keys() |
33 { | 42 { |
34 return Array.from(this[internal].map.keys()).map(ext.getPage); | 43 return Array.from(this[internal].map.keys()).map(ext.getPage); |
35 }, | 44 }, |
36 get(page) | 45 get(page) |
37 { | 46 { |
38 return this[internal].map.get(page.id); | 47 return this[internal].map.get(page.id); |
39 }, | 48 }, |
40 set(page, value) | 49 set(page, value) |
41 { | 50 { |
42 this[internal].map.set(page.id, value); | 51 this[internal].map.set(page.id, value); |
43 nonEmptyPageMaps.add(this); | 52 nonEmptyPageMaps.add(this); |
44 }, | 53 }, |
45 has(page) | 54 has(page) |
46 { | 55 { |
47 return this[internal].map.has(page.id); | 56 return this[internal].map.has(page.id); |
48 }, | 57 }, |
49 clear() | 58 clear() |
50 { | 59 { |
51 this[internal].map.clear(); | 60 this[internal].map.clear(); |
52 nonEmptyPageMaps.delete(this); | 61 nonEmptyPageMaps.delete(this); |
53 }, | 62 }, |
54 delete(page) | 63 delete(page) |
55 { | 64 { |
56 deletePage(this, page.id); | 65 this[internal].delete(page.id); |
57 } | 66 } |
58 }; | 67 }; |
59 | |
60 function deletePage(pageMap, pageId) | |
61 { | |
62 pageMap[internal].map.delete(pageId); | |
63 | |
64 if (pageMap[internal].map.size == 0) | |
65 nonEmptyPageMaps.delete(pageMap); | |
66 } | |
67 | 68 |
68 ext[internal].removeFromAllPageMaps = pageId => | 69 ext[internal].removeFromAllPageMaps = pageId => |
69 { | 70 { |
70 for (let pageMap of nonEmptyPageMaps) | 71 for (let pageMap of nonEmptyPageMaps) |
71 deletePage(pageMap, pageId); | 72 pageMap[internal].delete(pageId); |
72 }; | 73 }; |
73 | 74 |
74 /* Pages */ | 75 /* Pages */ |
75 | 76 |
76 let Page = ext.Page = function(tab) | 77 let Page = ext.Page = function(tab) |
77 { | 78 { |
78 defineNamespace(this, internal); | 79 defineNamespace(this, internal); |
79 | 80 |
80 this.id = tab.id; | 81 this.id = tab.id; |
81 | 82 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 130 |
130 ext.pages = { | 131 ext.pages = { |
131 onLoading: new ext[internal].EventTarget(), | 132 onLoading: new ext[internal].EventTarget(), |
132 onActivated: new ext[internal].EventTarget(), | 133 onActivated: new ext[internal].EventTarget(), |
133 onRemoved: new ext[internal].EventTarget() | 134 onRemoved: new ext[internal].EventTarget() |
134 }; | 135 }; |
135 | 136 |
136 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => | 137 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => |
137 { | 138 { |
138 if (changeInfo.status == "loading") | 139 if (changeInfo.status == "loading") |
139 ext[internal].dispatchEvent(ext.pages.onLoading, new Page(tab)); | 140 ext.pages.onLoading[internal].dispatch(new Page(tab)); |
140 }); | 141 }); |
141 | 142 |
142 function createFrame(tabId, frameId) | 143 function createFrame(tabId, frameId) |
143 { | 144 { |
144 let frames = framesOfTabs.get(tabId); | 145 let frames = framesOfTabs.get(tabId); |
145 if (!frames) | 146 if (!frames) |
146 { | 147 { |
147 frames = new Map(); | 148 frames = new Map(); |
148 framesOfTabs.set(tabId, frames); | 149 framesOfTabs.set(tabId, frames); |
149 } | 150 } |
(...skipping 18 matching lines...) Expand all Loading... |
168 | 169 |
169 browser.tabs.get(tabId, () => | 170 browser.tabs.get(tabId, () => |
170 { | 171 { |
171 // If the tab is prerendered, browser.tabs.get() sets | 172 // If the tab is prerendered, browser.tabs.get() sets |
172 // browser.runtime.lastError and we have to dispatch the onLoading | 173 // browser.runtime.lastError and we have to dispatch the onLoading |
173 // event, since the onUpdated event isn't dispatched for prerendered | 174 // event, since the onUpdated event isn't dispatched for prerendered |
174 // tabs. However, we have to keep relying on the onUpdated event for | 175 // tabs. However, we have to keep relying on the onUpdated event for |
175 // tabs that are already visible. Otherwise browser action changes get | 176 // tabs that are already visible. Otherwise browser action changes get |
176 // overridden when Chrome automatically resets them on navigation. | 177 // overridden when Chrome automatically resets them on navigation. |
177 if (browser.runtime.lastError) | 178 if (browser.runtime.lastError) |
178 ext[internal].dispatchEvent(ext.pages.onLoading, page); | 179 ext.pages.onLoading[internal].dispatch(page); |
179 }); | 180 }); |
180 } | 181 } |
181 | 182 |
182 // Update frame URL and parent in frame structure | 183 // Update frame URL and parent in frame structure |
183 let frame = createFrame(tabId, frameId); | 184 let frame = createFrame(tabId, frameId); |
184 frame.url = new URL(url); | 185 frame.url = new URL(url); |
185 | 186 |
186 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId); | 187 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId); |
187 if (parentFrame) | 188 if (parentFrame) |
188 frame.parent = parentFrame; | 189 frame.parent = parentFrame; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 // for Web Store URLs. | 279 // for Web Store URLs. |
279 !url.startsWith("https://chrome.google.com/webstore/"))) | 280 !url.startsWith("https://chrome.google.com/webstore/"))) |
280 { | 281 { |
281 updatePageFrameStructure(details.frameId, details.tabId, url, | 282 updatePageFrameStructure(details.frameId, details.tabId, url, |
282 details.parentFrameId); | 283 details.parentFrameId); |
283 } | 284 } |
284 }); | 285 }); |
285 | 286 |
286 function forgetTab(tabId) | 287 function forgetTab(tabId) |
287 { | 288 { |
288 ext[internal].dispatchEvent(ext.pages.onRemoved, tabId); | 289 ext.pages.onRemoved[internal].dispatch(tabId); |
289 | 290 |
290 ext[internal].removeFromAllPageMaps(tabId); | 291 ext[internal].removeFromAllPageMaps(tabId); |
291 framesOfTabs.delete(tabId); | 292 framesOfTabs.delete(tabId); |
292 } | 293 } |
293 | 294 |
294 browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => | 295 browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => |
295 { | 296 { |
296 forgetTab(removedTabId); | 297 forgetTab(removedTabId); |
297 }); | 298 }); |
298 | 299 |
299 browser.tabs.onRemoved.addListener(forgetTab); | 300 browser.tabs.onRemoved.addListener(forgetTab); |
300 | 301 |
301 browser.tabs.onActivated.addListener(details => | 302 browser.tabs.onActivated.addListener(details => |
302 { | 303 { |
303 ext[internal].dispatchEvent(ext.pages.onActivated, | 304 ext.pages.onActivated[internal].dispatch(new Page({id: details.tabId})); |
304 new Page({id: details.tabId})); | |
305 }); | 305 }); |
306 | 306 |
307 | 307 |
308 /* Browser actions */ | 308 /* Browser actions */ |
309 | 309 |
310 let BrowserAction = function(tabId) | 310 let BrowserAction = function(tabId) |
311 { | 311 { |
312 defineNamespace(this, internal); | 312 defineNamespace(this, internal); |
313 | 313 |
314 this[internal].tabId = tabId; | 314 this[internal].tabId = tabId; |
315 this[internal].changes = null; | 315 this[internal].changes = null; |
316 }; | 316 }; |
317 BrowserAction.prototype = { | 317 BrowserAction.prototype = { |
| 318 [internal]: { |
| 319 applyChanges() |
| 320 { |
| 321 if ("iconPath" in this[internal].changes) |
| 322 { |
| 323 // Firefox for Android displays the browser action not as an icon but |
| 324 // as a menu item. There is no icon, but such an option may be added |
| 325 // in the future. |
| 326 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 |
| 327 if ("setIcon" in browser.browserAction) |
| 328 { |
| 329 let path = { |
| 330 16: this[internal].changes.iconPath.replace("$size", "16"), |
| 331 19: this[internal].changes.iconPath.replace("$size", "19"), |
| 332 20: this[internal].changes.iconPath.replace("$size", "20"), |
| 333 32: this[internal].changes.iconPath.replace("$size", "32"), |
| 334 38: this[internal].changes.iconPath.replace("$size", "38"), |
| 335 40: this[internal].changes.iconPath.replace("$size", "40") |
| 336 }; |
| 337 try |
| 338 { |
| 339 browser.browserAction.setIcon({ |
| 340 tabId: this[internal].tabId, |
| 341 path |
| 342 }); |
| 343 } |
| 344 catch (e) |
| 345 { |
| 346 // Edge throws if passed icon sizes different than 19,20,38,40px. |
| 347 delete path[16]; |
| 348 delete path[32]; |
| 349 browser.browserAction.setIcon({ |
| 350 tabId: this[internal].tabId, |
| 351 path |
| 352 }); |
| 353 } |
| 354 } |
| 355 } |
| 356 |
| 357 if ("badgeText" in this[internal].changes) |
| 358 { |
| 359 // There is no badge on Firefox for Android; the browser action is |
| 360 // simply a menu item. |
| 361 if ("setBadgeText" in browser.browserAction) |
| 362 { |
| 363 browser.browserAction.setBadgeText({ |
| 364 tabId: this[internal].tabId, |
| 365 text: this[internal].changes.badgeText |
| 366 }); |
| 367 } |
| 368 } |
| 369 |
| 370 if ("badgeColor" in this[internal].changes) |
| 371 { |
| 372 // There is no badge on Firefox for Android; the browser action is |
| 373 // simply a menu item. |
| 374 if ("setBadgeBackgroundColor" in browser.browserAction) |
| 375 { |
| 376 browser.browserAction.setBadgeBackgroundColor({ |
| 377 tabId: this[internal].tabId, |
| 378 color: this[internal].changes.badgeColor |
| 379 }); |
| 380 } |
| 381 } |
| 382 |
| 383 this[internal].changes = null; |
| 384 }, |
| 385 queueChanges() |
| 386 { |
| 387 browser.tabs.get(this[internal].tabId, () => |
| 388 { |
| 389 // If the tab is prerendered, browser.tabs.get() sets |
| 390 // browser.runtime.lastError and we have to delay our changes |
| 391 // until the currently visible tab is replaced with the |
| 392 // prerendered tab. Otherwise browser.browserAction.set* fails. |
| 393 if (browser.runtime.lastError) |
| 394 { |
| 395 let onReplaced = (addedTabId, removedTabId) => |
| 396 { |
| 397 if (addedTabId == this[internal].tabId) |
| 398 { |
| 399 browser.tabs.onReplaced.removeListener(onReplaced); |
| 400 this[internal].applyChanges(); |
| 401 } |
| 402 }; |
| 403 browser.tabs.onReplaced.addListener(onReplaced); |
| 404 } |
| 405 else |
| 406 { |
| 407 this[internal].applyChanges(); |
| 408 } |
| 409 }); |
| 410 }, |
| 411 addChange(name, value) |
| 412 { |
| 413 if (!this[internal].changes) |
| 414 { |
| 415 this[internal].changes = {}; |
| 416 this[internal].queueChanges(); |
| 417 } |
| 418 |
| 419 this[internal].changes[name] = value; |
| 420 } |
| 421 }, |
318 setIcon(path) | 422 setIcon(path) |
319 { | 423 { |
320 addBrowserActionChange(this, "iconPath", path); | 424 this[internal].addChange("iconPath", path); |
321 }, | 425 }, |
322 setBadge(badge) | 426 setBadge(badge) |
323 { | 427 { |
324 if (!badge) | 428 if (!badge) |
325 { | 429 { |
326 addBrowserActionChange(this, "badgeText", ""); | 430 this[internal].addChange("badgeText", ""); |
327 } | 431 } |
328 else | 432 else |
329 { | 433 { |
330 if ("number" in badge) | 434 if ("number" in badge) |
331 addBrowserActionChange(this, "badgeText", badge.number.toString()); | 435 this[internal].addChange("badgeText", badge.number.toString()); |
332 | 436 |
333 if ("color" in badge) | 437 if ("color" in badge) |
334 addBrowserActionChange(this, "badgeColor", badge.color); | 438 this[internal].addChange("badgeColor", badge.color); |
335 } | 439 } |
336 } | 440 } |
337 }; | 441 }; |
338 | |
339 function queueBrowserActionChanges(browserAction) | |
340 { | |
341 browser.tabs.get(browserAction[internal].tabId, () => | |
342 { | |
343 // If the tab is prerendered, browser.tabs.get() sets | |
344 // browser.runtime.lastError and we have to delay our changes | |
345 // until the currently visible tab is replaced with the | |
346 // prerendered tab. Otherwise browser.browserAction.set* fails. | |
347 if (browser.runtime.lastError) | |
348 { | |
349 let onReplaced = (addedTabId, removedTabId) => | |
350 { | |
351 if (addedTabId == browserAction[internal].tabId) | |
352 { | |
353 browser.tabs.onReplaced.removeListener(onReplaced); | |
354 applyBrowserActionChanges(browserAction); | |
355 } | |
356 }; | |
357 browser.tabs.onReplaced.addListener(onReplaced); | |
358 } | |
359 else | |
360 { | |
361 applyBrowserActionChanges(browserAction); | |
362 } | |
363 }); | |
364 } | |
365 | |
366 function applyBrowserActionChanges(browserAction) | |
367 { | |
368 if ("iconPath" in browserAction[internal].changes) | |
369 { | |
370 // Firefox for Android displays the browser action not as an icon but | |
371 // as a menu item. There is no icon, but such an option may be added in | |
372 // the future. | |
373 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 | |
374 if ("setIcon" in browser.browserAction) | |
375 { | |
376 let path = { | |
377 16: browserAction[internal].changes.iconPath.replace("$size", "16"), | |
378 19: browserAction[internal].changes.iconPath.replace("$size", "19"), | |
379 20: browserAction[internal].changes.iconPath.replace("$size", "20"), | |
380 32: browserAction[internal].changes.iconPath.replace("$size", "32"), | |
381 38: browserAction[internal].changes.iconPath.replace("$size", "38"), | |
382 40: browserAction[internal].changes.iconPath.replace("$size", "40") | |
383 }; | |
384 try | |
385 { | |
386 browser.browserAction.setIcon({ | |
387 tabId: browserAction[internal].tabId, | |
388 path | |
389 }); | |
390 } | |
391 catch (e) | |
392 { | |
393 // Edge throws if passed icon sizes different than 19,20,38,40px. | |
394 delete path[16]; | |
395 delete path[32]; | |
396 browser.browserAction.setIcon({ | |
397 tabId: browserAction[internal].tabId, | |
398 path | |
399 }); | |
400 } | |
401 } | |
402 } | |
403 | |
404 if ("badgeText" in browserAction[internal].changes) | |
405 { | |
406 // There is no badge on Firefox for Android; the browser action is | |
407 // simply a menu item. | |
408 if ("setBadgeText" in browser.browserAction) | |
409 { | |
410 browser.browserAction.setBadgeText({ | |
411 tabId: browserAction[internal].tabId, | |
412 text: browserAction[internal].changes.badgeText | |
413 }); | |
414 } | |
415 } | |
416 | |
417 if ("badgeColor" in browserAction[internal].changes) | |
418 { | |
419 // There is no badge on Firefox for Android; the browser action is | |
420 // simply a menu item. | |
421 if ("setBadgeBackgroundColor" in browser.browserAction) | |
422 { | |
423 browser.browserAction.setBadgeBackgroundColor({ | |
424 tabId: browserAction[internal].tabId, | |
425 color: browserAction[internal].changes.badgeColor | |
426 }); | |
427 } | |
428 } | |
429 | |
430 browserAction[internal].changes = null; | |
431 } | |
432 | |
433 function addBrowserActionChange(browserAction, name, value) | |
434 { | |
435 if (!browserAction[internal].changes) | |
436 { | |
437 browserAction[internal].changes = {}; | |
438 queueBrowserActionChanges(browserAction); | |
439 } | |
440 | |
441 browserAction[internal].changes[name] = value; | |
442 } | |
443 | 442 |
444 | 443 |
445 /* Context menus */ | 444 /* Context menus */ |
446 | 445 |
447 let contextMenuItems = new ext.PageMap(); | 446 let contextMenuItems = new ext.PageMap(); |
448 let contextMenuUpdating = false; | 447 let contextMenuUpdating = false; |
449 | 448 |
450 let updateContextMenu = () => | 449 let updateContextMenu = () => |
451 { | 450 { |
452 // Firefox for Android does not support context menus. | 451 // Firefox for Android does not support context menus. |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 // Sometimes requests are not associated with a browser tab and | 636 // Sometimes requests are not associated with a browser tab and |
638 // in this case we want to still be able to view the url being called. | 637 // in this case we want to still be able to view the url being called. |
639 let frame = null; | 638 let frame = null; |
640 let page = null; | 639 let page = null; |
641 if (details.tabId != -1) | 640 if (details.tabId != -1) |
642 { | 641 { |
643 frame = ext.getFrame(details.tabId, frameId); | 642 frame = ext.getFrame(details.tabId, frameId); |
644 page = new Page({id: details.tabId}); | 643 page = new Page({id: details.tabId}); |
645 } | 644 } |
646 | 645 |
647 if (ext[internal].dispatchEvent(ext.webRequest.onBeforeRequest, | 646 if (ext.webRequest.onBeforeRequest[internal] |
648 url, type, page, frame).includes(false)) | 647 .dispatch(url, type, page, frame).includes(false)) |
649 { | 648 { |
650 return {cancel: true}; | 649 return {cancel: true}; |
651 } | 650 } |
652 }, {urls: ["<all_urls>"]}, ["blocking"]); | 651 }, {urls: ["<all_urls>"]}, ["blocking"]); |
653 | 652 |
654 | 653 |
655 /* Message passing */ | 654 /* Message passing */ |
656 | 655 |
657 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => | 656 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => |
658 { | 657 { |
(...skipping 18 matching lines...) Expand all Loading... |
677 | 676 |
678 let frame = frames.get(rawSender.frameId); | 677 let frame = frames.get(rawSender.frameId); |
679 if (frame) | 678 if (frame) |
680 return frame.parent || null; | 679 return frame.parent || null; |
681 | 680 |
682 return frames.get(0) || null; | 681 return frames.get(0) || null; |
683 } | 682 } |
684 }; | 683 }; |
685 } | 684 } |
686 | 685 |
687 return ext[internal].dispatchEvent( | 686 return ext.onMessage[internal].dispatch( |
688 ext.onMessage, | |
689 message, sender, sendResponse | 687 message, sender, sendResponse |
690 ).includes(true); | 688 ).includes(true); |
691 }); | 689 }); |
692 | 690 |
693 | 691 |
694 /* Storage */ | 692 /* Storage */ |
695 | 693 |
696 ext.storage = { | 694 ext.storage = { |
697 get(keys, callback) | 695 get(keys, callback) |
698 { | 696 { |
(...skipping 16 matching lines...) Expand all Loading... |
715 ext.windows = { | 713 ext.windows = { |
716 create(createData, callback) | 714 create(createData, callback) |
717 { | 715 { |
718 browser.windows.create(createData, createdWindow => | 716 browser.windows.create(createData, createdWindow => |
719 { | 717 { |
720 afterTabLoaded(callback)(createdWindow.tabs[0]); | 718 afterTabLoaded(callback)(createdWindow.tabs[0]); |
721 }); | 719 }); |
722 } | 720 } |
723 }; | 721 }; |
724 } | 722 } |
LEFT | RIGHT |