| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 function getAbsolutePosition(elt) { | 183 function getAbsolutePosition(elt) { |
| 184 var l = 0; | 184 var l = 0; |
| 185 var t = 0; | 185 var t = 0; |
| 186 for(; elt; elt = elt.offsetParent) { | 186 for(; elt; elt = elt.offsetParent) { |
| 187 l += elt.offsetLeft; | 187 l += elt.offsetLeft; |
| 188 t += elt.offsetTop; | 188 t += elt.offsetTop; |
| 189 } | 189 } |
| 190 return [l, t]; | 190 return [l, t]; |
| 191 } | 191 } |
| 192 | 192 |
| 193 function isDisplayed(element) | |
| 194 { | |
| 195 while (element) | |
| 196 { | |
| 197 if (getComputedStyle(element).display == "none") | |
| 198 return false; | |
| 199 | |
| 200 element = element.parentElement; | |
| 201 } | |
| 202 | |
| 203 return true; | |
| 204 } | |
| 205 | |
| 206 // Adds an overlay to an element, which is probably a Flash object | 193 // Adds an overlay to an element, which is probably a Flash object |
| 207 function addElementOverlay(elt) { | 194 function addElementOverlay(elt) { |
| 208 // If this element is enclosed in an object tag, we prefer to block that inste ad | 195 // If this element is enclosed in an object tag, we prefer to block that inste ad |
| 209 if(!elt) | 196 if(!elt) |
| 210 return null; | 197 return null; |
| 211 | 198 |
| 212 // If element doesn't have at least one of class name, ID or URL, give up | 199 // If element doesn't have at least one of class name, ID or URL, give up |
| 213 // because we don't know how to construct a filter rule for it | 200 // because we don't know how to construct a filter rule for it |
| 214 if(!hasFilters(elt)) | 201 if(!hasFilters(elt)) |
| 215 return; | 202 return; |
| 216 | 203 |
| 217 // If the element isn't rendered (since its or one of its ancestor's | 204 // If the element isn't rendered (since its or one of its ancestor's |
| 218 // "diplay" property is "none"), the overlay wouldn't match the element. | 205 // "diplay" property is "none"), the overlay wouldn't match the element. |
|
Wladimir Palant
2014/11/26 13:10:18
Nit: "display"
| |
| 219 if (!isDisplayed(elt)) | 206 if (!elt.offsetParent) |
|
Wladimir Palant
2014/11/26 13:10:18
What about elements that use visibility:hidden or
Sebastian Noack
2014/11/26 13:15:07
We don't have to bother about those. In that case
| |
| 220 return; | 207 return; |
| 221 | 208 |
| 222 var thisStyle = getComputedStyle(elt, null); | 209 var thisStyle = getComputedStyle(elt, null); |
| 223 var overlay = document.createElement('div'); | 210 var overlay = document.createElement('div'); |
| 224 overlay.prisoner = elt; | 211 overlay.prisoner = elt; |
| 225 overlay.className = "__adblockplus__overlay"; | 212 overlay.className = "__adblockplus__overlay"; |
| 226 overlay.setAttribute('style', 'opacity:0.4; background-color:#ffffff; display: inline-box; ' + 'width:' + thisStyle.width + '; height:' + thisStyle.height + '; position:absolute; overflow:hidden; -webkit-box-sizing:border-box; z-index: 999 99'); | 213 overlay.setAttribute('style', 'opacity:0.4; background-color:#ffffff; display: inline-box; ' + 'width:' + thisStyle.width + '; height:' + thisStyle.height + '; position:absolute; overflow:hidden; -webkit-box-sizing:border-box; z-index: 999 99'); |
| 227 var pos = getAbsolutePosition(elt); | 214 var pos = getAbsolutePosition(elt); |
| 228 overlay.style.left = pos[0] + "px"; | 215 overlay.style.left = pos[0] + "px"; |
| 229 overlay.style.top = pos[1] + "px"; | 216 overlay.style.top = pos[1] + "px"; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 // If we are already selecting, abort now | 267 // If we are already selecting, abort now |
| 281 if (clickHide_activated || clickHideFiltersDialog) | 268 if (clickHide_activated || clickHideFiltersDialog) |
| 282 clickHide_deactivate(); | 269 clickHide_deactivate(); |
| 283 | 270 |
| 284 // Add overlays for elements with URLs so user can easily click them | 271 // Add overlays for elements with URLs so user can easily click them |
| 285 var elts = document.querySelectorAll('object,embed,img,iframe,video,audio,pict ure'); | 272 var elts = document.querySelectorAll('object,embed,img,iframe,video,audio,pict ure'); |
| 286 for(var i=0; i<elts.length; i++) | 273 for(var i=0; i<elts.length; i++) |
| 287 addElementOverlay(elts[i]); | 274 addElementOverlay(elts[i]); |
| 288 | 275 |
| 289 clickHide_activated = true; | 276 clickHide_activated = true; |
| 290 document.addEventListener("mouseover", clickHide_mouseOver, false); | 277 document.addEventListener("mouseover", clickHide_mouseOver, true); |
| 291 document.addEventListener("mouseout", clickHide_mouseOut, false); | 278 document.addEventListener("mouseout", clickHide_mouseOut, true); |
| 292 document.addEventListener("click", clickHide_mouseClick, false); | 279 document.addEventListener("click", clickHide_mouseClick, true); |
| 293 document.addEventListener("keydown", clickHide_keyDown, false); | 280 document.addEventListener("keydown", clickHide_keyDown, true); |
| 294 } | 281 } |
| 295 | 282 |
| 296 // Called when user has clicked on something and we are waiting for confirmation | 283 // Called when user has clicked on something and we are waiting for confirmation |
| 297 // on whether the user actually wants these filters | 284 // on whether the user actually wants these filters |
| 298 function clickHide_rulesPending() { | 285 function clickHide_rulesPending() { |
| 299 clickHide_activated = false; | 286 clickHide_activated = false; |
| 300 document.removeEventListener("mouseover", clickHide_mouseOver, false); | 287 document.removeEventListener("mouseover", clickHide_mouseOver, true); |
| 301 document.removeEventListener("mouseout", clickHide_mouseOut, false); | 288 document.removeEventListener("mouseout", clickHide_mouseOut, true); |
| 302 document.removeEventListener("click", clickHide_mouseClick, false); | 289 document.removeEventListener("click", clickHide_mouseClick, true); |
| 303 document.removeEventListener("keydown", clickHide_keyDown, false); | 290 document.removeEventListener("keydown", clickHide_keyDown, true); |
| 304 } | 291 } |
| 305 | 292 |
| 306 // Turn off click-to-hide | 293 // Turn off click-to-hide |
| 307 function clickHide_deactivate() | 294 function clickHide_deactivate() |
| 308 { | 295 { |
| 309 if (clickHideFiltersDialog) | 296 if (clickHideFiltersDialog) |
| 310 { | 297 { |
| 311 document.body.removeChild(clickHideFiltersDialog); | 298 document.body.removeChild(clickHideFiltersDialog); |
| 312 clickHideFiltersDialog = null; | 299 clickHideFiltersDialog = null; |
| 313 } | 300 } |
| 314 | 301 |
| 315 if(currentElement) { | 302 if(currentElement) { |
| 316 currentElement.removeEventListener("contextmenu", clickHide_elementClickHand ler, false); | 303 currentElement.removeEventListener("contextmenu", clickHide_elementClickHand ler, false); |
| 317 unhighlightElements(); | 304 unhighlightElements(); |
| 318 unhighlightElement(currentElement); | 305 unhighlightElement(currentElement); |
| 319 currentElement = null; | 306 currentElement = null; |
| 320 clickHideFilters = null; | 307 clickHideFilters = null; |
| 321 } | 308 } |
| 322 unhighlightElements(); | 309 unhighlightElements(); |
| 323 | 310 |
| 324 clickHide_activated = false; | 311 clickHide_activated = false; |
| 325 clickHide_filters = null; | 312 clickHide_filters = null; |
| 326 if(!document) | 313 if(!document) |
| 327 return; // This can happen inside a nuked iframe...I think | 314 return; // This can happen inside a nuked iframe...I think |
| 328 document.removeEventListener("mouseover", clickHide_mouseOver, false); | 315 document.removeEventListener("mouseover", clickHide_mouseOver, true); |
| 329 document.removeEventListener("mouseout", clickHide_mouseOut, false); | 316 document.removeEventListener("mouseout", clickHide_mouseOut, true); |
| 330 document.removeEventListener("click", clickHide_mouseClick, false); | 317 document.removeEventListener("click", clickHide_mouseClick, true); |
| 331 document.removeEventListener("keydown", clickHide_keyDown, false); | 318 document.removeEventListener("keydown", clickHide_keyDown, true); |
| 332 | 319 |
| 333 // Remove overlays | 320 // Remove overlays |
| 334 // For some reason iterating over the array returend by getElementsByClassName () doesn't work | 321 // For some reason iterating over the array returend by getElementsByClassName () doesn't work |
| 335 var elt; | 322 var elt; |
| 336 while(elt = document.querySelector('.__adblockplus__overlay')) | 323 while(elt = document.querySelector('.__adblockplus__overlay')) |
| 337 elt.parentNode.removeChild(elt); | 324 elt.parentNode.removeChild(elt); |
| 338 } | 325 } |
| 339 | 326 |
| 340 function clickHide_elementClickHandler(ev) { | 327 function clickHide_elementClickHandler(ev) { |
| 341 ev.preventDefault(); | 328 ev.preventDefault(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 353 while (target.parentNode && !hasFilters(target)) | 340 while (target.parentNode && !hasFilters(target)) |
| 354 target = target.parentNode; | 341 target = target.parentNode; |
| 355 if (target == document.documentElement || target == document.body) | 342 if (target == document.documentElement || target == document.body) |
| 356 target = null; | 343 target = null; |
| 357 | 344 |
| 358 if (target && target instanceof HTMLElement) | 345 if (target && target instanceof HTMLElement) |
| 359 { | 346 { |
| 360 currentElement = target; | 347 currentElement = target; |
| 361 | 348 |
| 362 highlightElement(target, "#d6d84b", "#f8fa47"); | 349 highlightElement(target, "#d6d84b", "#f8fa47"); |
| 363 target.addEventListener("contextmenu", clickHide_elementClickHandler, false) ; | 350 target.addEventListener("contextmenu", clickHide_elementClickHandler, true); |
| 364 } | 351 } |
| 365 } | 352 } |
| 366 | 353 |
| 367 // No longer hovering over this element so unhighlight it | 354 // No longer hovering over this element so unhighlight it |
| 368 function clickHide_mouseOut(e) | 355 function clickHide_mouseOut(e) |
| 369 { | 356 { |
| 370 if (!clickHide_activated || !currentElement) | 357 if (!clickHide_activated || !currentElement) |
| 371 return; | 358 return; |
| 372 | 359 |
| 373 unhighlightElement(currentElement); | 360 unhighlightElement(currentElement); |
| 374 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, false); | 361 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, true); |
| 375 } | 362 } |
| 376 | 363 |
| 377 // Selects the currently hovered-over filter or cancels selection | 364 // Selects the currently hovered-over filter or cancels selection |
| 378 function clickHide_keyDown(e) | 365 function clickHide_keyDown(e) |
| 379 { | 366 { |
| 380 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /) | 367 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /) |
| 381 clickHide_mouseClick(e); | 368 clickHide_mouseClick(e); |
| 382 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/) | 369 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/) |
| 383 { | 370 { |
| 384 ext.backgroundPage.sendMessage( | 371 ext.backgroundPage.sendMessage( |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 608 // exists before continuing to avoid "Uncaught ReferenceError: ext is not define d". | 595 // exists before continuing to avoid "Uncaught ReferenceError: ext is not define d". |
| 609 // See https://crbug.com/416907 | 596 // See https://crbug.com/416907 |
| 610 if ("ext" in window && document instanceof HTMLDocument) | 597 if ("ext" in window && document instanceof HTMLDocument) |
| 611 { | 598 { |
| 612 // Use a contextmenu handler to save the last element the user right-clicked o n. | 599 // Use a contextmenu handler to save the last element the user right-clicked o n. |
| 613 // To make things easier, we actually save the DOM event. | 600 // To make things easier, we actually save the DOM event. |
| 614 // We have to do this because the contextMenu API only provides a URL, not the actual | 601 // We have to do this because the contextMenu API only provides a URL, not the actual |
| 615 // DOM element. | 602 // DOM element. |
| 616 document.addEventListener('contextmenu', function(e) { | 603 document.addEventListener('contextmenu', function(e) { |
| 617 lastRightClickEvent = e; | 604 lastRightClickEvent = e; |
| 618 }, false); | 605 }, true); |
| 619 | 606 |
| 620 document.addEventListener("click", function(event) | 607 document.addEventListener("click", function(event) |
| 621 { | 608 { |
| 622 // Ignore right-clicks | 609 // Ignore right-clicks |
| 623 if (event.button == 2) | 610 if (event.button == 2) |
| 624 return; | 611 return; |
| 625 | 612 |
| 626 // Search the link associated with the click | 613 // Search the link associated with the click |
| 627 var link = event.target; | 614 var link = event.target; |
| 628 while (link && !(link instanceof HTMLAnchorElement)) | 615 while (link && !(link instanceof HTMLAnchorElement)) |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 765 break; | 752 break; |
| 766 default: | 753 default: |
| 767 sendResponse({}); | 754 sendResponse({}); |
| 768 break; | 755 break; |
| 769 } | 756 } |
| 770 }); | 757 }); |
| 771 | 758 |
| 772 if (window == window.top) | 759 if (window == window.top) |
| 773 ext.backgroundPage.sendMessage({type: "report-html-page"}); | 760 ext.backgroundPage.sendMessage({type: "report-html-page"}); |
| 774 } | 761 } |
| LEFT | RIGHT |