Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: background.js

Issue 8741005: Topic 11337 - More reliable approach to hide image/frame placeholders (Closed)
Patch Set: Created Oct. 31, 2012, 9:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include.preload.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of the Adblock Plus extension, 2 * This file is part of the Adblock Plus extension,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 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 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 chrome.tabs.onUpdated.removeListener(listener); 460 chrome.tabs.onUpdated.removeListener(listener);
461 onLoad(); 461 onLoad();
462 } 462 }
463 }; 463 };
464 chrome.tabs.onUpdated.addListener(listener); 464 chrome.tabs.onUpdated.addListener(listener);
465 } 465 }
466 }); 466 });
467 } 467 }
468 } 468 }
469 469
470 /**
471 * This function is a hack - we only know the tabId and document URL for a
472 * message but we need to know the frame ID. Try to find it in webRequest's
473 * frame data.
474 */
475 function getFrameId(tabId, url)
476 {
477 if (tabId in frames)
478 {
479 for (var f in frames[tabId])
480 {
481 if (getFrameUrl(tabId, f) == url)
482 return f;
483 }
484 }
485 return -1;
486 }
487
470 chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 488 chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
471 { 489 {
472 switch (request.reqtype) 490 switch (request.reqtype)
473 { 491 {
474 case "get-settings": 492 case "get-settings":
475 var hostDomain = null; 493 var hostDomain = null;
476 var selectors = null; 494 var selectors = null;
477 495
478 // HACK: We don't know which frame sent us the message, try to find it
479 // in webRequest's frame data.
480 var tabId = -1; 496 var tabId = -1;
481 var frameId = -1; 497 var frameId = -1;
482 if (sender.tab) 498 if (sender.tab)
483 { 499 {
484 tabId = sender.tab.id; 500 tabId = sender.tab.id;
485 if (tabId in frames) 501 frameId = getFrameId(tabId, request.frameUrl);
486 {
487 for (var f in frames[tabId])
488 {
489 if (getFrameUrl(tabId, f) == request.frameUrl)
490 {
491 frameId = f;
492 break;
493 }
494 }
495 }
496 } 502 }
497 503
498 var enabled = !isFrameWhitelisted(tabId, frameId, "DOCUMENT") && !isFrameW hitelisted(tabId, frameId, "ELEMHIDE"); 504 var enabled = !isFrameWhitelisted(tabId, frameId, "DOCUMENT") && !isFrameW hitelisted(tabId, frameId, "ELEMHIDE");
499 if (enabled && request.selectors) 505 if (enabled && request.selectors)
500 { 506 {
501 var noStyleRules = false; 507 var noStyleRules = false;
502 var host = extractHostFromURL(request.frameUrl); 508 var host = extractHostFromURL(request.frameUrl);
503 hostDomain = getBaseDomain(host); 509 hostDomain = getBaseDomain(host);
504 for (var i = 0; i < noStyleRulesHosts.length; i++) 510 for (var i = 0; i < noStyleRulesHosts.length; i++)
505 { 511 {
506 var noStyleHost = noStyleRulesHosts[i]; 512 var noStyleHost = noStyleRulesHosts[i];
507 if (host == noStyleHost || (host.length > noStyleHost.length && 513 if (host == noStyleHost || (host.length > noStyleHost.length &&
508 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost)) 514 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
509 { 515 {
510 noStyleRules = true; 516 noStyleRules = true;
511 } 517 }
512 } 518 }
513 selectors = ElemHide.getSelectorsForDomain(host, false); 519 selectors = ElemHide.getSelectorsForDomain(host, false);
514 if (noStyleRules) 520 if (noStyleRules)
515 { 521 {
516 selectors = selectors.filter(function(s) 522 selectors = selectors.filter(function(s)
517 { 523 {
518 return !/\[style[\^\$]?=/.test(s); 524 return !/\[style[\^\$]?=/.test(s);
519 }); 525 });
520 } 526 }
521 } 527 }
522 528
523 sendResponse({enabled: enabled, hostDomain: hostDomain, selectors: selecto rs}); 529 sendResponse({enabled: enabled, hostDomain: hostDomain, selectors: selecto rs});
524 break; 530 break;
531 case "should-collapse":
532 var tabId = -1;
533 var frameId = -1;
534 if (sender.tab)
535 {
536 tabId = sender.tab.id;
537 frameId = getFrameId(tabId, request.documentUrl);
538 }
539
540 var enabled = !isFrameWhitelisted(tabId, frameId, "DOCUMENT");
541 if (!enabled)
Thomas Greiner 2012/10/31 11:32:00 very confusing why not simply like that: if (isFr
Wladimir Palant 2012/10/31 12:17:53 That's code copied from element hiding handling ab
542 {
543 sendResponse(false);
544 break;
545 }
546
547 var requestHost = extractHostFromURL(request.url);
548 var documentHost = extractHostFromURL(request.documentUrl);
549 var thirdParty = isThirdParty(requestHost, documentHost);
550 var filter = defaultMatcher.matchesAny(request.url, request.type, document Host, thirdParty);
551 if (filter instanceof BlockingFilter)
552 {
553 var collapse = filter.collapse;
554 if (collapse == null)
555 collapse = (localStorage.hidePlaceholders != "false");
556 sendResponse(collapse);
557 }
558 else
559 sendResponse(false);
560 break;
525 case "get-domain-enabled-state": 561 case "get-domain-enabled-state":
526 // Returns whether this domain is in the exclusion list. 562 // Returns whether this domain is in the exclusion list.
527 // The page action popup asks us this. 563 // The page action popup asks us this.
528 if(sender.tab) 564 if(sender.tab)
529 { 565 {
530 sendResponse({enabled: !isWhitelisted(sender.tab.url), specialCaseYouTub e: localStorage["specialCaseYouTube"] == "true", disableInlineTextAds: localStor age["disableInlineTextAds"] == "true"}); 566 sendResponse({enabled: !isWhitelisted(sender.tab.url), specialCaseYouTub e: localStorage["specialCaseYouTube"] == "true", disableInlineTextAds: localStor age["disableInlineTextAds"] == "true"});
531 return; 567 return;
532 } 568 }
533 break; 569 break;
534 case "add-filters": 570 case "add-filters":
(...skipping 26 matching lines...) Expand all
561 refreshIconAndContextMenu(windows[i].tabs[j]); 597 refreshIconAndContextMenu(windows[i].tabs[j]);
562 }); 598 });
563 599
564 // Update icon if a tab changes location 600 // Update icon if a tab changes location
565 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 601 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
566 { 602 {
567 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 603 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
568 if(changeInfo.status == "loading") 604 if(changeInfo.status == "loading")
569 refreshIconAndContextMenu(tab); 605 refreshIconAndContextMenu(tab);
570 }); 606 });
OLDNEW
« no previous file with comments | « no previous file | include.preload.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld