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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 130 |
131 ext.pages = { | 131 ext.pages = { |
132 onLoading: new ext[internal].EventTarget(), | 132 onLoading: new ext[internal].EventTarget(), |
133 onActivated: new ext[internal].EventTarget(), | 133 onActivated: new ext[internal].EventTarget(), |
134 onRemoved: new ext[internal].EventTarget() | 134 onRemoved: new ext[internal].EventTarget() |
135 }; | 135 }; |
136 | 136 |
137 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => | 137 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => |
138 { | 138 { |
139 if (changeInfo.status == "loading") | 139 if (changeInfo.status == "loading") |
140 ext[internal].dispatchEvent(ext.pages.onLoading, new Page(tab)); | 140 ext.pages.onLoading[internal].dispatch(new Page(tab)); |
141 }); | 141 }); |
142 | 142 |
143 function createFrame(tabId, frameId) | 143 function createFrame(tabId, frameId) |
144 { | 144 { |
145 let frames = framesOfTabs.get(tabId); | 145 let frames = framesOfTabs.get(tabId); |
146 if (!frames) | 146 if (!frames) |
147 { | 147 { |
148 frames = new Map(); | 148 frames = new Map(); |
149 framesOfTabs.set(tabId, frames); | 149 framesOfTabs.set(tabId, frames); |
150 } | 150 } |
(...skipping 18 matching lines...) Expand all Loading... |
169 | 169 |
170 browser.tabs.get(tabId, () => | 170 browser.tabs.get(tabId, () => |
171 { | 171 { |
172 // If the tab is prerendered, browser.tabs.get() sets | 172 // If the tab is prerendered, browser.tabs.get() sets |
173 // browser.runtime.lastError and we have to dispatch the onLoading | 173 // browser.runtime.lastError and we have to dispatch the onLoading |
174 // event, since the onUpdated event isn't dispatched for prerendered | 174 // event, since the onUpdated event isn't dispatched for prerendered |
175 // 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 |
176 // tabs that are already visible. Otherwise browser action changes get | 176 // tabs that are already visible. Otherwise browser action changes get |
177 // overridden when Chrome automatically resets them on navigation. | 177 // overridden when Chrome automatically resets them on navigation. |
178 if (browser.runtime.lastError) | 178 if (browser.runtime.lastError) |
179 ext[internal].dispatchEvent(ext.pages.onLoading, page); | 179 ext.pages.onLoading[internal].dispatch(page); |
180 }); | 180 }); |
181 } | 181 } |
182 | 182 |
183 // Update frame URL and parent in frame structure | 183 // Update frame URL and parent in frame structure |
184 let frame = createFrame(tabId, frameId); | 184 let frame = createFrame(tabId, frameId); |
185 frame.url = new URL(url); | 185 frame.url = new URL(url); |
186 | 186 |
187 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId); | 187 let parentFrame = framesOfTabs.get(tabId).get(parentFrameId); |
188 if (parentFrame) | 188 if (parentFrame) |
189 frame.parent = parentFrame; | 189 frame.parent = parentFrame; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // for Web Store URLs. | 279 // for Web Store URLs. |
280 !url.startsWith("https://chrome.google.com/webstore/"))) | 280 !url.startsWith("https://chrome.google.com/webstore/"))) |
281 { | 281 { |
282 updatePageFrameStructure(details.frameId, details.tabId, url, | 282 updatePageFrameStructure(details.frameId, details.tabId, url, |
283 details.parentFrameId); | 283 details.parentFrameId); |
284 } | 284 } |
285 }); | 285 }); |
286 | 286 |
287 function forgetTab(tabId) | 287 function forgetTab(tabId) |
288 { | 288 { |
289 ext[internal].dispatchEvent(ext.pages.onRemoved, tabId); | 289 ext.pages.onRemoved[internal].dispatch(tabId); |
290 | 290 |
291 ext[internal].removeFromAllPageMaps(tabId); | 291 ext[internal].removeFromAllPageMaps(tabId); |
292 framesOfTabs.delete(tabId); | 292 framesOfTabs.delete(tabId); |
293 } | 293 } |
294 | 294 |
295 browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => | 295 browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => |
296 { | 296 { |
297 forgetTab(removedTabId); | 297 forgetTab(removedTabId); |
298 }); | 298 }); |
299 | 299 |
300 browser.tabs.onRemoved.addListener(forgetTab); | 300 browser.tabs.onRemoved.addListener(forgetTab); |
301 | 301 |
302 browser.tabs.onActivated.addListener(details => | 302 browser.tabs.onActivated.addListener(details => |
303 { | 303 { |
304 ext[internal].dispatchEvent(ext.pages.onActivated, | 304 ext.pages.onActivated[internal].dispatch(new Page({id: details.tabId})); |
305 new Page({id: details.tabId})); | |
306 }); | 305 }); |
307 | 306 |
308 | 307 |
309 /* Browser actions */ | 308 /* Browser actions */ |
310 | 309 |
311 let BrowserAction = function(tabId) | 310 let BrowserAction = function(tabId) |
312 { | 311 { |
313 defineNamespace(this, internal); | 312 defineNamespace(this, internal); |
314 | 313 |
315 this[internal].tabId = tabId; | 314 this[internal].tabId = tabId; |
316 this[internal].changes = null; | 315 this[internal].changes = null; |
317 }; | 316 }; |
318 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 }, |
319 setIcon(path) | 422 setIcon(path) |
320 { | 423 { |
321 addBrowserActionChange(this, "iconPath", path); | 424 this[internal].addChange("iconPath", path); |
322 }, | 425 }, |
323 setBadge(badge) | 426 setBadge(badge) |
324 { | 427 { |
325 if (!badge) | 428 if (!badge) |
326 { | 429 { |
327 addBrowserActionChange(this, "badgeText", ""); | 430 this[internal].addChange("badgeText", ""); |
328 } | 431 } |
329 else | 432 else |
330 { | 433 { |
331 if ("number" in badge) | 434 if ("number" in badge) |
332 addBrowserActionChange(this, "badgeText", badge.number.toString()); | 435 this[internal].addChange("badgeText", badge.number.toString()); |
333 | 436 |
334 if ("color" in badge) | 437 if ("color" in badge) |
335 addBrowserActionChange(this, "badgeColor", badge.color); | 438 this[internal].addChange("badgeColor", badge.color); |
336 } | 439 } |
337 } | 440 } |
338 }; | 441 }; |
339 | |
340 function queueBrowserActionChanges(browserAction) | |
341 { | |
342 browser.tabs.get(browserAction[internal].tabId, () => | |
343 { | |
344 // If the tab is prerendered, browser.tabs.get() sets | |
345 // browser.runtime.lastError and we have to delay our changes | |
346 // until the currently visible tab is replaced with the | |
347 // prerendered tab. Otherwise browser.browserAction.set* fails. | |
348 if (browser.runtime.lastError) | |
349 { | |
350 let onReplaced = (addedTabId, removedTabId) => | |
351 { | |
352 if (addedTabId == browserAction[internal].tabId) | |
353 { | |
354 browser.tabs.onReplaced.removeListener(onReplaced); | |
355 applyBrowserActionChanges(browserAction); | |
356 } | |
357 }; | |
358 browser.tabs.onReplaced.addListener(onReplaced); | |
359 } | |
360 else | |
361 { | |
362 applyBrowserActionChanges(browserAction); | |
363 } | |
364 }); | |
365 } | |
366 | |
367 function applyBrowserActionChanges(browserAction) | |
368 { | |
369 if ("iconPath" in browserAction[internal].changes) | |
370 { | |
371 // Firefox for Android displays the browser action not as an icon but | |
372 // as a menu item. There is no icon, but such an option may be added in | |
373 // the future. | |
374 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 | |
375 if ("setIcon" in browser.browserAction) | |
376 { | |
377 let path = { | |
378 16: browserAction[internal].changes.iconPath.replace("$size", "16"), | |
379 19: browserAction[internal].changes.iconPath.replace("$size", "19"), | |
380 20: browserAction[internal].changes.iconPath.replace("$size", "20"), | |
381 32: browserAction[internal].changes.iconPath.replace("$size", "32"), | |
382 38: browserAction[internal].changes.iconPath.replace("$size", "38"), | |
383 40: browserAction[internal].changes.iconPath.replace("$size", "40") | |
384 }; | |
385 try | |
386 { | |
387 browser.browserAction.setIcon({ | |
388 tabId: browserAction[internal].tabId, | |
389 path | |
390 }); | |
391 } | |
392 catch (e) | |
393 { | |
394 // Edge throws if passed icon sizes different than 19,20,38,40px. | |
395 delete path[16]; | |
396 delete path[32]; | |
397 browser.browserAction.setIcon({ | |
398 tabId: browserAction[internal].tabId, | |
399 path | |
400 }); | |
401 } | |
402 } | |
403 } | |
404 | |
405 if ("badgeText" in browserAction[internal].changes) | |
406 { | |
407 // There is no badge on Firefox for Android; the browser action is | |
408 // simply a menu item. | |
409 if ("setBadgeText" in browser.browserAction) | |
410 { | |
411 browser.browserAction.setBadgeText({ | |
412 tabId: browserAction[internal].tabId, | |
413 text: browserAction[internal].changes.badgeText | |
414 }); | |
415 } | |
416 } | |
417 | |
418 if ("badgeColor" in browserAction[internal].changes) | |
419 { | |
420 // There is no badge on Firefox for Android; the browser action is | |
421 // simply a menu item. | |
422 if ("setBadgeBackgroundColor" in browser.browserAction) | |
423 { | |
424 browser.browserAction.setBadgeBackgroundColor({ | |
425 tabId: browserAction[internal].tabId, | |
426 color: browserAction[internal].changes.badgeColor | |
427 }); | |
428 } | |
429 } | |
430 | |
431 browserAction[internal].changes = null; | |
432 } | |
433 | |
434 function addBrowserActionChange(browserAction, name, value) | |
435 { | |
436 if (!browserAction[internal].changes) | |
437 { | |
438 browserAction[internal].changes = {}; | |
439 queueBrowserActionChanges(browserAction); | |
440 } | |
441 | |
442 browserAction[internal].changes[name] = value; | |
443 } | |
444 | 442 |
445 | 443 |
446 /* Context menus */ | 444 /* Context menus */ |
447 | 445 |
448 let contextMenuItems = new ext.PageMap(); | 446 let contextMenuItems = new ext.PageMap(); |
449 let contextMenuUpdating = false; | 447 let contextMenuUpdating = false; |
450 | 448 |
451 let updateContextMenu = () => | 449 let updateContextMenu = () => |
452 { | 450 { |
453 // 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... |
638 // Sometimes requests are not associated with a browser tab and | 636 // Sometimes requests are not associated with a browser tab and |
639 // 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. |
640 let frame = null; | 638 let frame = null; |
641 let page = null; | 639 let page = null; |
642 if (details.tabId != -1) | 640 if (details.tabId != -1) |
643 { | 641 { |
644 frame = ext.getFrame(details.tabId, frameId); | 642 frame = ext.getFrame(details.tabId, frameId); |
645 page = new Page({id: details.tabId}); | 643 page = new Page({id: details.tabId}); |
646 } | 644 } |
647 | 645 |
648 if (ext[internal].dispatchEvent(ext.webRequest.onBeforeRequest, | 646 if (ext.webRequest.onBeforeRequest[internal] |
649 url, type, page, frame).includes(false)) | 647 .dispatch(url, type, page, frame).includes(false)) |
650 { | 648 { |
651 return {cancel: true}; | 649 return {cancel: true}; |
652 } | 650 } |
653 }, {urls: ["<all_urls>"]}, ["blocking"]); | 651 }, {urls: ["<all_urls>"]}, ["blocking"]); |
654 | 652 |
655 | 653 |
656 /* Message passing */ | 654 /* Message passing */ |
657 | 655 |
658 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => | 656 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => |
659 { | 657 { |
(...skipping 18 matching lines...) Expand all Loading... |
678 | 676 |
679 let frame = frames.get(rawSender.frameId); | 677 let frame = frames.get(rawSender.frameId); |
680 if (frame) | 678 if (frame) |
681 return frame.parent || null; | 679 return frame.parent || null; |
682 | 680 |
683 return frames.get(0) || null; | 681 return frames.get(0) || null; |
684 } | 682 } |
685 }; | 683 }; |
686 } | 684 } |
687 | 685 |
688 return ext[internal].dispatchEvent( | 686 return ext.onMessage[internal].dispatch( |
689 ext.onMessage, | |
690 message, sender, sendResponse | 687 message, sender, sendResponse |
691 ).includes(true); | 688 ).includes(true); |
692 }); | 689 }); |
693 | 690 |
694 | 691 |
695 /* Storage */ | 692 /* Storage */ |
696 | 693 |
697 ext.storage = { | 694 ext.storage = { |
698 get(keys, callback) | 695 get(keys, callback) |
699 { | 696 { |
(...skipping 16 matching lines...) Expand all Loading... |
716 ext.windows = { | 713 ext.windows = { |
717 create(createData, callback) | 714 create(createData, callback) |
718 { | 715 { |
719 browser.windows.create(createData, createdWindow => | 716 browser.windows.create(createData, createdWindow => |
720 { | 717 { |
721 afterTabLoaded(callback)(createdWindow.tabs[0]); | 718 afterTabLoaded(callback)(createdWindow.tabs[0]); |
722 }); | 719 }); |
723 } | 720 } |
724 }; | 721 }; |
725 } | 722 } |
LEFT | RIGHT |