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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 ext.getFrame = function(tabId, frameId) | 324 ext.getFrame = function(tabId, frameId) |
325 { | 325 { |
326 return (framesOfTabs[tabId] || {})[frameId]; | 326 return (framesOfTabs[tabId] || {})[frameId]; |
327 }; | 327 }; |
328 | 328 |
329 ext.webRequest = { | 329 ext.webRequest = { |
330 onBeforeRequest: new ext._EventTarget(), | 330 onBeforeRequest: new ext._EventTarget(), |
331 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged | 331 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged |
332 }; | 332 }; |
333 | 333 |
| 334 // Since Chrome 38 requests of type 'object' (e.g. requests |
| 335 // initiated by Flash) are mistakenly reported with the type 'other'. |
| 336 // https://code.google.com/p/chromium/issues/detail?id=410382 |
| 337 if (parseInt(navigator.userAgent.match(/\bChrome\/(\d+)/)[1], 10) >= 38) |
| 338 { |
| 339 ext.webRequest.indistinguishableTypes = [ |
| 340 ["OTHER", "OBJECT", "OBJECT_SUBREQUEST"] |
| 341 ]; |
| 342 } |
| 343 else |
| 344 { |
| 345 ext.webRequest.indistinguishableTypes = [ |
| 346 ["OBJECT", "OBJECT_SUBREQUEST"], |
| 347 ["OTHER", "MEDIA", "FONT"] |
| 348 ]; |
| 349 } |
| 350 |
334 chrome.tabs.query({}, function(tabs) | 351 chrome.tabs.query({}, function(tabs) |
335 { | 352 { |
336 tabs.forEach(function(tab) | 353 tabs.forEach(function(tab) |
337 { | 354 { |
338 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details) | 355 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details) |
339 { | 356 { |
340 if (details && details.length > 0) | 357 if (details && details.length > 0) |
341 { | 358 { |
342 var frames = framesOfTabs[tab.id] = Object.create(null); | 359 var frames = framesOfTabs[tab.id] = Object.create(null); |
343 | 360 |
(...skipping 15 matching lines...) Expand all Loading... |
359 chrome.webRequest.onBeforeRequest.addListener(function(details) | 376 chrome.webRequest.onBeforeRequest.addListener(function(details) |
360 { | 377 { |
361 try | 378 try |
362 { | 379 { |
363 // the high-level code isn't interested in requests that aren't related | 380 // the high-level code isn't interested in requests that aren't related |
364 // to a tab and since those can only be handled in Chrome, we ignore | 381 // to a tab and since those can only be handled in Chrome, we ignore |
365 // them here instead of in the browser independent high-level code. | 382 // them here instead of in the browser independent high-level code. |
366 if (details.tabId == -1) | 383 if (details.tabId == -1) |
367 return; | 384 return; |
368 | 385 |
369 var requestType = details.type; | 386 var isMainFrame = details.type == "main_frame" || ( |
370 var isMainFrame = requestType == "main_frame" || ( | |
371 | 387 |
372 // assume that the first request belongs to the top frame. Chrome | 388 // assume that the first request belongs to the top frame. Chrome |
373 // may give the top frame the type "object" instead of "main_frame". | 389 // may give the top frame the type "object" instead of "main_frame". |
374 // https://code.google.com/p/chromium/issues/detail?id=281711 | 390 // https://code.google.com/p/chromium/issues/detail?id=281711 |
375 details.frameId == 0 && !(details.tabId in framesOfTabs) | 391 details.frameId == 0 && !(details.tabId in framesOfTabs) |
376 ); | 392 ); |
377 | 393 |
378 var frames = null; | 394 var frames = null; |
379 if (!isMainFrame) | 395 if (!isMainFrame) |
380 frames = framesOfTabs[details.tabId]; | 396 frames = framesOfTabs[details.tabId]; |
381 if (!frames) | 397 if (!frames) |
382 frames = framesOfTabs[details.tabId] = Object.create(null); | 398 frames = framesOfTabs[details.tabId] = Object.create(null); |
383 | 399 |
384 var frame = null; | 400 var frame = null; |
385 var url = new URL(details.url); | 401 var url = new URL(details.url); |
386 if (!isMainFrame) | 402 if (!isMainFrame) |
387 { | 403 { |
388 // we are looking for the frame that contains the element that | 404 // we are looking for the frame that contains the element that |
389 // is about to load, however if a frame is loading the surrounding | 405 // is about to load, however if a frame is loading the surrounding |
390 // frame is indicated by parentFrameId instead of frameId | 406 // frame is indicated by parentFrameId instead of frameId |
391 var frameId; | 407 var frameId; |
392 if (requestType == "sub_frame") | 408 var requestType; |
| 409 if (details.type == "sub_frame") |
| 410 { |
393 frameId = details.parentFrameId; | 411 frameId = details.parentFrameId; |
| 412 requestType = "SUBDOCUMENT"; |
| 413 } |
394 else | 414 else |
| 415 { |
395 frameId = details.frameId; | 416 frameId = details.frameId; |
| 417 requestType = details.type.toUpperCase(); |
| 418 } |
396 | 419 |
397 frame = frames[frameId] || frames[Object.keys(frames)[0]]; | 420 frame = frames[frameId] || frames[Object.keys(frames)[0]]; |
398 | 421 |
399 if (frame) | 422 if (frame) |
400 { | 423 { |
401 // Since Chrome 38 requests of type 'object' (e.g. requests | |
402 // initiated by Flash) are mistakenly reported with the type 'other'. | |
403 // https://code.google.com/p/chromium/issues/detail?id=410382 | |
404 if (requestType == "other" && parseInt(navigator.userAgent.match(/\bCh
rome\/(\d+)/)[1], 10) >= 38) | |
405 requestType = "object"; | |
406 | |
407 var results = ext.webRequest.onBeforeRequest._dispatch( | 424 var results = ext.webRequest.onBeforeRequest._dispatch( |
408 url, | 425 url, |
409 requestType, | 426 requestType, |
410 new Page({id: details.tabId}), | 427 new Page({id: details.tabId}), |
411 frame | 428 frame |
412 ); | 429 ); |
413 | 430 |
414 if (results.indexOf(false) != -1) | 431 if (results.indexOf(false) != -1) |
415 return {cancel: true}; | 432 return {cancel: true}; |
416 } | 433 } |
417 } | 434 } |
418 | 435 |
419 if (isMainFrame || details.type == "sub_frame") | 436 if (isMainFrame || details.type == "sub_frame") |
420 frames[details.frameId] = {url: url, parent: frame}; | 437 frames[details.frameId] = {url: url, parent: frame}; |
421 } | 438 } |
422 catch (e) | 439 catch (e) |
423 { | 440 { |
424 // recent versions of Chrome cancel the request when an error occurs in | 441 // recent versions of Chrome cancel the request when an error occurs in |
425 // the onBeforeRequest listener. However in our case it is preferred, to | 442 // the onBeforeRequest listener. However in our case it is preferred, to |
426 // let potentially some ads through, rather than blocking legit requests. | 443 // let potentially some ads through, rather than blocking legit requests. |
427 console.error(e); | 444 console.error(e); |
428 } | 445 } |
429 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); | 446 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); |
430 | 447 |
431 | 448 |
432 /* Message passing */ | 449 /* Message passing */ |
433 | 450 |
434 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse
) | 451 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse
) |
435 { | 452 { |
436 var sender = { | 453 var sender = {}; |
437 page: new Page(rawSender.tab), | 454 |
438 frame: { | 455 // Add "page" and "frame" if the message was sent by a content script. |
| 456 // If sent by popup or the background page itself, there is no "tab". |
| 457 if ("tab" in rawSender) |
| 458 { |
| 459 sender.page = new Page(rawSender.tab); |
| 460 sender.frame = { |
439 url: new URL(rawSender.url), | 461 url: new URL(rawSender.url), |
440 get parent() | 462 get parent() |
441 { | 463 { |
442 var frames = framesOfTabs[rawSender.tab.id]; | 464 var frames = framesOfTabs[rawSender.tab.id]; |
443 | 465 |
444 if (!frames) | 466 if (!frames) |
445 return null; | 467 return null; |
446 | 468 |
447 if ("frameId" in rawSender) | 469 if ("frameId" in rawSender) |
448 { | 470 { |
449 // Chrome 41+ | 471 // Chrome 41+ |
450 var frame = frames[rawSender.frameId]; | 472 var frame = frames[rawSender.frameId]; |
451 if (frame) | 473 if (frame) |
452 return frame.parent; | 474 return frame.parent; |
453 } | 475 } |
454 else | 476 else |
455 { | 477 { |
456 // Chrome 28-40 | 478 // Chrome 28-40 |
457 for (var frameId in frames) | 479 for (var frameId in frames) |
458 { | 480 { |
459 if (frames[frameId].url.href == this.url.href) | 481 if (frames[frameId].url.href == this.url.href) |
460 return frames[frameId].parent; | 482 return frames[frameId].parent; |
461 } | 483 } |
462 } | 484 } |
463 | 485 |
464 return frames[0]; | 486 return frames[0]; |
465 } | 487 } |
466 } | 488 }; |
467 }; | 489 } |
468 | 490 |
469 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true)
!= -1; | 491 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true)
!= -1; |
470 }); | 492 }); |
471 | 493 |
472 // We have to ensure there is at least one listener for the onConnect event. | 494 // We have to ensure there is at least one listener for the onConnect event. |
473 // Otherwise we can't connect a port later, which we need to do in order to | 495 // Otherwise we can't connect a port later, which we need to do in order to |
474 // detect when the extension is reloaded, disabled or uninstalled. | 496 // detect when the extension is reloaded, disabled or uninstalled. |
475 chrome.runtime.onConnect.addListener(function() {}); | 497 chrome.runtime.onConnect.addListener(function() {}); |
476 | 498 |
477 | 499 |
(...skipping 11 matching lines...) Expand all Loading... |
489 chrome.storage.local.set(items, callback); | 511 chrome.storage.local.set(items, callback); |
490 }, | 512 }, |
491 remove: function(key, callback) | 513 remove: function(key, callback) |
492 { | 514 { |
493 chrome.storage.local.remove(key, callback); | 515 chrome.storage.local.remove(key, callback); |
494 }, | 516 }, |
495 onChanged: chrome.storage.onChanged, | 517 onChanged: chrome.storage.onChanged, |
496 | 518 |
497 // Migrate localStorage to chrome.storage.local, | 519 // Migrate localStorage to chrome.storage.local, |
498 // ignoring unkown and inavlid preferences. | 520 // ignoring unkown and inavlid preferences. |
499 migratePrefs: function(prefs) | 521 migratePrefs: function(hooks) |
500 { | 522 { |
501 var items = {}; | 523 var items = {}; |
502 | 524 |
503 for (let key in localStorage) | 525 for (let key in localStorage) |
504 { | 526 { |
505 var value = localStorage[key]; | 527 var item = hooks.map(key, localStorage[key]); |
506 | 528 if (item) |
507 if (key in prefs) | 529 items[item.key] = item.value; |
508 { | |
509 try | |
510 { | |
511 items["pref:" + key] = JSON.parse(value); | |
512 } | |
513 catch (e) | |
514 { | |
515 continue; | |
516 } | |
517 } | |
518 else if (key == "currentVersion") | |
519 { | |
520 items[key] = value; | |
521 } | |
522 } | 530 } |
523 | 531 |
524 chrome.storage.local.set(items, function() { | 532 chrome.storage.local.set(items, function() { |
525 localStorage.clear(); | 533 localStorage.clear(); |
| 534 hooks.done(); |
526 }); | 535 }); |
527 }, | 536 }, |
528 | 537 |
529 // Migrate FileSystem API to chrome.storage.local. For simplicity | 538 // Migrate FileSystem API to chrome.storage.local. For simplicity |
530 // only patterns.ini is considered. Backups are left behind. | 539 // only patterns.ini is considered. Backups are left behind. |
531 migrateFiles: function(callback) | 540 migrateFiles: function(callback) |
532 { | 541 { |
533 if ("webkitRequestFileSystem" in window) | 542 if ("webkitRequestFileSystem" in window) |
534 { | 543 { |
535 webkitRequestFileSystem(PERSISTENT, 0, function(fs) | 544 webkitRequestFileSystem(PERSISTENT, 0, function(fs) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 if (!win.incognito) | 600 if (!win.incognito) |
592 queryInfo.windowId = win.id; | 601 queryInfo.windowId = win.id; |
593 | 602 |
594 chrome.tabs.query(queryInfo, function(tabs) | 603 chrome.tabs.query(queryInfo, function(tabs) |
595 { | 604 { |
596 if (tabs.length > 0) | 605 if (tabs.length > 0) |
597 { | 606 { |
598 var tab = tabs[0]; | 607 var tab = tabs[0]; |
599 | 608 |
600 chrome.windows.update(tab.windowId, {focused: true}); | 609 chrome.windows.update(tab.windowId, {focused: true}); |
601 chrome.tabs.highlight( | 610 chrome.tabs.update(tab.id, {active: true}); |
602 { | |
603 windowId: tab.windowId, | |
604 tabs: [tab.index] | |
605 }, | |
606 function() {} | |
607 ); | |
608 | 611 |
609 if (callback) | 612 if (callback) |
610 callback(new Page(tab)); | 613 callback(new Page(tab)); |
611 } | 614 } |
612 else | 615 else |
613 { | 616 { |
614 ext.pages.open(optionsUrl, callback); | 617 ext.pages.open(optionsUrl, callback); |
615 } | 618 } |
616 }); | 619 }); |
617 }); | 620 }); |
618 }; | 621 }; |
619 })(); | 622 })(); |
LEFT | RIGHT |