Left: | ||
Right: |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 // Click-to-hide stuff | 18 // Click-to-hide stuff |
19 var clickHide_activated = false; | 19 var clickHide_activated = false; |
20 var clickHide_filters = null; | 20 var clickHide_filters = null; |
21 var currentElement = null; | 21 var currentElement = null; |
22 var highlightedElementsSelector = null; | 22 var highlightedElementsSelector = null; |
23 var clickHideFiltersDialog = null; | 23 var clickHideFiltersDialog = null; |
24 var lastRightClickEvent = null; | 24 var lastRightClickEvent = null; |
25 var lastRightClickEventValid = false; | 25 var lastRightClickEventValid = false; |
26 var lastMouseOverEvent = null; | |
26 | 27 |
27 function highlightElement(element, shadowColor, backgroundColor) | 28 function highlightElement(element, shadowColor, backgroundColor) |
28 { | 29 { |
29 unhighlightElement(element); | 30 unhighlightElement(element); |
30 | 31 |
31 var highlightWithOverlay = function() | 32 var highlightWithOverlay = function() |
32 { | 33 { |
33 var overlay = addElementOverlay(element); | 34 var overlay = addElementOverlay(element); |
34 | 35 |
35 // If the element isn't displayed no overlay will be added. | 36 // If the element isn't displayed no overlay will be added. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 case "object": | 190 case "object": |
190 return getURLsFromObjectElement(element); | 191 return getURLsFromObjectElement(element); |
191 | 192 |
192 case "video": | 193 case "video": |
193 case "audio": | 194 case "audio": |
194 case "picture": | 195 case "picture": |
195 return getURLsFromMediaElement(element); | 196 return getURLsFromMediaElement(element); |
196 } | 197 } |
197 | 198 |
198 return getURLsFromAttributes(element); | 199 return getURLsFromAttributes(element); |
199 } | |
200 | |
201 function isBlockable(element) | |
202 { | |
203 if (element.id) | |
204 return true; | |
205 if (element.classList.length > 0) | |
206 return true; | |
207 if (getURLsFromElement(element).length > 0) | |
208 return true; | |
209 | |
210 // We only generate filters based on the "style" attribute, | |
211 // if this is the only way we can generate a filter, and | |
212 // only if there are at least two CSS properties defined. | |
213 if (/:.+:/.test(element.getAttribute("style"))) | |
214 return true; | |
215 | |
216 return false; | |
217 } | 200 } |
218 | 201 |
219 // Adds an overlay to an element, which is probably a Flash object | 202 // Adds an overlay to an element, which is probably a Flash object |
220 function addElementOverlay(elt) { | 203 function addElementOverlay(elt) { |
221 var zIndex = "auto"; | 204 var zIndex = "auto"; |
222 var position = "absolute"; | 205 var position = "absolute"; |
223 | 206 |
224 for (var e = elt; e; e = e.parentElement) | 207 for (var e = elt; e; e = e.parentElement) |
225 { | 208 { |
226 var style = getComputedStyle(e); | 209 var style = getComputedStyle(e); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 // elt.parentNode.appendChild(overlay, elt); | 246 // elt.parentNode.appendChild(overlay, elt); |
264 document.documentElement.appendChild(overlay); | 247 document.documentElement.appendChild(overlay); |
265 return overlay; | 248 return overlay; |
266 } | 249 } |
267 | 250 |
268 // Show dialog asking user whether she wants to add the proposed filters derived | 251 // Show dialog asking user whether she wants to add the proposed filters derived |
269 // from selected page element | 252 // from selected page element |
270 function clickHide_showDialog(left, top, filters) | 253 function clickHide_showDialog(left, top, filters) |
271 { | 254 { |
272 // If we are already selecting, abort now | 255 // If we are already selecting, abort now |
273 if (clickHide_activated || clickHideFiltersDialog) | 256 if (clickHide_activated) |
274 clickHide_rulesPending(); | 257 clickHide_rulesPending(); |
Wladimir Palant
2015/03/04 12:47:16
What if we go here because clickHideFiltersDialog
Sebastian Noack
2015/03/04 13:36:38
You are right, checking for clickHideFiltersDialog
| |
275 | 258 |
276 clickHide_filters = filters; | 259 clickHide_filters = filters; |
277 | 260 |
278 clickHideFiltersDialog = document.createElement("iframe"); | 261 clickHideFiltersDialog = document.createElement("iframe"); |
279 clickHideFiltersDialog.src = ext.getURL("block.html"); | 262 clickHideFiltersDialog.src = ext.getURL("block.html"); |
280 clickHideFiltersDialog.setAttribute("style", "position: fixed !important; visi bility: hidden; display: block !important; border: 0px !important;"); | 263 clickHideFiltersDialog.setAttribute("style", "position: fixed !important; visi bility: hidden; display: block !important; border: 0px !important;"); |
281 clickHideFiltersDialog.style.WebkitBoxShadow = "5px 5px 20px rgba(0,0,0,0.5)"; | 264 clickHideFiltersDialog.style.WebkitBoxShadow = "5px 5px 20px rgba(0,0,0,0.5)"; |
282 clickHideFiltersDialog.style.zIndex = 0x7FFFFFFF; | 265 clickHideFiltersDialog.style.zIndex = 0x7FFFFFFF; |
283 | 266 |
284 // Position in upper-left all the time | 267 // Position in upper-left all the time |
(...skipping 20 matching lines...) Expand all Loading... | |
305 function clickHide_activate() { | 288 function clickHide_activate() { |
306 if(document == null) | 289 if(document == null) |
307 return; | 290 return; |
308 | 291 |
309 // If we are already selecting, abort now | 292 // If we are already selecting, abort now |
310 if (clickHide_activated || clickHideFiltersDialog) | 293 if (clickHide_activated || clickHideFiltersDialog) |
311 clickHide_deactivate(); | 294 clickHide_deactivate(); |
312 | 295 |
313 // Add overlays for blockable elements that don't emit mouse events, | 296 // Add overlays for blockable elements that don't emit mouse events, |
314 // so that they can still be selected. | 297 // so that they can still be selected. |
315 var elts = document.querySelectorAll('object,embed,iframe,frame'); | 298 [].forEach.call( |
316 for(var i=0; i<elts.length; i++) | 299 document.querySelectorAll('object,embed,iframe,frame'), |
317 { | 300 function(element) |
318 var element = elts[i]; | 301 { |
319 if (isBlockable(element)) | 302 getFiltersForElement(element, function(filters) |
320 addElementOverlay(element); | 303 { |
321 } | 304 if (filters.length > 0) |
305 addElementOverlay(element); | |
306 }); | |
307 } | |
308 ); | |
322 | 309 |
323 clickHide_activated = true; | 310 clickHide_activated = true; |
324 document.addEventListener("mousedown", clickHide_stopPropagation, true); | 311 document.addEventListener("mousedown", clickHide_stopPropagation, true); |
325 document.addEventListener("mouseup", clickHide_stopPropagation, true); | 312 document.addEventListener("mouseup", clickHide_stopPropagation, true); |
326 document.addEventListener("mouseenter", clickHide_stopPropagation, true); | 313 document.addEventListener("mouseenter", clickHide_stopPropagation, true); |
327 document.addEventListener("mouseleave", clickHide_stopPropagation, true); | 314 document.addEventListener("mouseleave", clickHide_stopPropagation, true); |
328 document.addEventListener("mouseover", clickHide_mouseOver, true); | 315 document.addEventListener("mouseover", clickHide_mouseOver, true); |
329 document.addEventListener("mouseout", clickHide_mouseOut, true); | 316 document.addEventListener("mouseout", clickHide_mouseOut, true); |
330 document.addEventListener("click", clickHide_mouseClick, true); | 317 document.addEventListener("click", clickHide_mouseClick, true); |
331 document.addEventListener("keydown", clickHide_keyDown, true); | 318 document.addEventListener("keydown", clickHide_keyDown, true); |
(...skipping 21 matching lines...) Expand all Loading... | |
353 | 340 |
354 if (clickHideFiltersDialog) | 341 if (clickHideFiltersDialog) |
355 { | 342 { |
356 document.documentElement.removeChild(clickHideFiltersDialog); | 343 document.documentElement.removeChild(clickHideFiltersDialog); |
357 clickHideFiltersDialog = null; | 344 clickHideFiltersDialog = null; |
358 } | 345 } |
359 | 346 |
360 clickHide_filters = null; | 347 clickHide_filters = null; |
361 lastRightClickEvent = null; | 348 lastRightClickEvent = null; |
362 | 349 |
363 if (currentElement) { | 350 if (currentElement) |
Wladimir Palant
2015/03/04 12:47:16
Nit: Opening bracket should go on the next line, w
Sebastian Noack
2015/03/04 13:36:38
Done.
| |
351 { | |
364 currentElement.removeEventListener("contextmenu", clickHide_elementClickHan dler, true); | 352 currentElement.removeEventListener("contextmenu", clickHide_elementClickHan dler, true); |
365 unhighlightElements(); | 353 unhighlightElements(); |
366 unhighlightElement(currentElement); | 354 unhighlightElement(currentElement); |
367 currentElement = null; | 355 currentElement = null; |
368 } | 356 } |
369 unhighlightElements(); | 357 unhighlightElements(); |
370 | 358 |
371 var overlays = document.getElementsByClassName("__adblockplus__overlay"); | 359 var overlays = document.getElementsByClassName("__adblockplus__overlay"); |
372 while (overlays.length > 0) | 360 while (overlays.length > 0) |
373 overlays[0].parentNode.removeChild(overlays[0]); | 361 overlays[0].parentNode.removeChild(overlays[0]); |
374 | 362 |
375 ext.onExtensionUnloaded.removeListener(clickHide_deactivate); | 363 ext.onExtensionUnloaded.removeListener(clickHide_deactivate); |
376 } | 364 } |
377 | 365 |
378 function clickHide_stopPropagation(e) | 366 function clickHide_stopPropagation(e) |
379 { | 367 { |
380 e.stopPropagation(); | 368 e.stopPropagation(); |
381 } | 369 } |
382 | 370 |
383 function clickHide_elementClickHandler(e) { | 371 function clickHide_elementClickHandler(e) { |
384 e.preventDefault(); | 372 e.preventDefault(); |
385 e.stopPropagation(); | 373 e.stopPropagation(); |
386 clickHide_mouseClick(e); | 374 clickHide_mouseClick(e); |
387 } | 375 } |
388 | 376 |
389 function getBlockableElementOrAncestor(element) | 377 function getBlockableElementOrAncestor(element, callback) |
390 { | 378 { |
379 // We assume that the user doesn't want to block the whole page. | |
380 // So we never consider the <html> or <body> element. | |
391 while (element && element != document.documentElement | 381 while (element && element != document.documentElement |
392 && element != document.body) | 382 && element != document.body) |
393 { | 383 { |
394 if (element instanceof HTMLElement && element.localName != "area") | 384 // We can't handle non-HTML (like SVG) elements, as well as |
395 { | 385 // <area> elements (see below). So fall back to the parent element. |
396 // Handle <area> and their <map> elements specially, | 386 if (!(element instanceof HTMLElement) || element.localName == "area") |
397 // blocking the image they are associated with | 387 element = element.parentElement; |
398 if (element.localName == "map") | 388 |
389 // If image maps are used mouse events occur for the <area> element. | |
390 // But we have to block the image associated with the <map> element. | |
391 else if (element.localName == "map") | |
392 { | |
393 var images = document.querySelectorAll("img[usemap]"); | |
394 var image = null; | |
395 | |
396 for (var i = 0; i < images.length; i++) | |
399 { | 397 { |
400 var images = document.querySelectorAll("img[usemap]"); | 398 var usemap = image.getAttribute("usemap"); |
401 for (var i = 0; i < images.length; i++) | 399 var index = usemap.indexOf("#"); |
400 | |
401 if (index != -1 && usemap.substr(index + 1) == element.name) | |
402 { | 402 { |
403 var image = images[i]; | 403 image = images[i]; |
404 var usemap = image.getAttribute("usemap"); | 404 break; |
405 var index = usemap.indexOf("#"); | |
406 | |
407 if (index != -1 && usemap.substr(index + 1) == element.name) | |
408 return getBlockableElementOrAncestor(image); | |
409 } | 405 } |
410 | |
411 return null; | |
412 } | 406 } |
413 | 407 |
414 if (isBlockable(element)) | 408 element = image; |
415 return element; | 409 } |
416 } | 410 |
417 | 411 // Finally, if none of the above is true, check whether we can generate |
418 element = element.parentElement; | 412 // any filters for this element. Otherwise fall back to its parent element. |
419 } | 413 else |
420 | 414 { |
421 return null; | 415 getFiltersForElement(element, function(filters) |
416 { | |
417 if (filters.length > 0) | |
418 callback(element); | |
419 else | |
420 getBlockableElementOrAncestor(element.parentElement, callback); | |
421 }); | |
422 | |
423 return; | |
424 } | |
425 } | |
426 | |
427 // We reached the document root without finding a blockable element. | |
428 callback(null); | |
422 } | 429 } |
423 | 430 |
424 // Hovering over an element so highlight it | 431 // Hovering over an element so highlight it |
425 function clickHide_mouseOver(e) | 432 function clickHide_mouseOver(e) |
426 { | 433 { |
427 if (clickHide_activated == false) | 434 lastMouseOverEvent = e; |
428 return; | 435 |
429 | 436 getBlockableElementOrAncestor(e.target, function(element) |
430 var target = getBlockableElementOrAncestor(e.target); | 437 { |
431 | 438 if (e == lastMouseOverEvent) |
432 if (target) | 439 { |
433 { | 440 lastMouseOverEvent = null; |
434 currentElement = target; | 441 |
435 | 442 if (clickHide_activated) |
436 highlightElement(target, "#d6d84b", "#f8fa47"); | 443 { |
437 target.addEventListener("contextmenu", clickHide_elementClickHandler, true); | 444 if (currentElement) |
438 } | 445 unhighlightElement(currentElement); |
446 | |
447 if (element) | |
448 { | |
449 highlightElement(element, "#d6d84b", "#f8fa47"); | |
450 element.addEventListener("contextmenu", clickHide_elementClickHandler, true); | |
451 } | |
452 | |
453 currentElement = element; | |
454 } | |
455 } | |
456 }); | |
457 | |
439 e.stopPropagation(); | 458 e.stopPropagation(); |
440 } | 459 } |
441 | 460 |
442 // No longer hovering over this element so unhighlight it | 461 // No longer hovering over this element so unhighlight it |
443 function clickHide_mouseOut(e) | 462 function clickHide_mouseOut(e) |
444 { | 463 { |
445 if (!clickHide_activated || !currentElement) | 464 if (!clickHide_activated || currentElement != e.target) |
446 return; | 465 return; |
447 | 466 |
448 unhighlightElement(currentElement); | 467 unhighlightElement(currentElement); |
449 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, true); | 468 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, true); |
450 e.stopPropagation(); | 469 e.stopPropagation(); |
451 } | 470 } |
452 | 471 |
453 // Selects the currently hovered-over filter or cancels selection | 472 // Selects the currently hovered-over filter or cancels selection |
454 function clickHide_keyDown(e) | 473 function clickHide_keyDown(e) |
455 { | 474 { |
(...skipping 18 matching lines...) Expand all Loading... | |
474 { | 493 { |
475 ext.backgroundPage.sendMessage( | 494 ext.backgroundPage.sendMessage( |
476 { | 495 { |
477 type: "compose-filters", | 496 type: "compose-filters", |
478 tagName: element.localName, | 497 tagName: element.localName, |
479 id: element.id, | 498 id: element.id, |
480 src: element.getAttribute("src"), | 499 src: element.getAttribute("src"), |
481 style: element.getAttribute("style"), | 500 style: element.getAttribute("style"), |
482 classes: [].slice.call(element.classList), | 501 classes: [].slice.call(element.classList), |
483 urls: getURLsFromElement(element), | 502 urls: getURLsFromElement(element), |
503 mediatype: typeMap[element.localName], | |
484 baseURL: document.location.href | 504 baseURL: document.location.href |
485 }, | 505 }, |
486 function(response) | 506 function(response) |
487 { | 507 { |
488 callback(response.filters, response.selectors); | 508 callback(response.filters, response.selectors); |
489 } | 509 } |
490 ); | 510 ); |
491 } | 511 } |
492 | 512 |
493 // When the user clicks, the currentElement is the one we want. | 513 // When the user clicks, the currentElement is the one we want. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
652 break; | 672 break; |
653 case "clickhide-activate": | 673 case "clickhide-activate": |
654 clickHide_activate(); | 674 clickHide_activate(); |
655 break; | 675 break; |
656 case "clickhide-deactivate": | 676 case "clickhide-deactivate": |
657 clickHide_deactivate(); | 677 clickHide_deactivate(); |
658 break; | 678 break; |
659 case "clickhide-new-filter": | 679 case "clickhide-new-filter": |
660 if(lastRightClickEvent) | 680 if(lastRightClickEvent) |
661 { | 681 { |
662 clickHide_activated = true; | 682 getBlockableElementOrAncestor(lastRightClickEvent.target, function(ele ment) |
663 currentElement = getBlockableElementOrAncestor(lastRightClickEvent.tar get); | 683 { |
664 clickHide_mouseClick(lastRightClickEvent); | 684 clickHide_activated = true; |
685 currentElement = element; | |
686 clickHide_mouseClick(lastRightClickEvent); | |
687 }); | |
665 } | 688 } |
666 break; | 689 break; |
667 case "clickhide-init": | 690 case "clickhide-init": |
668 if (clickHideFiltersDialog) | 691 if (clickHideFiltersDialog) |
669 { | 692 { |
670 sendResponse({filters: clickHide_filters}); | 693 sendResponse({filters: clickHide_filters}); |
671 | 694 |
672 clickHideFiltersDialog.style.width = msg.width + "px"; | 695 clickHideFiltersDialog.style.width = msg.width + "px"; |
673 clickHideFiltersDialog.style.height = msg.height + "px"; | 696 clickHideFiltersDialog.style.height = msg.height + "px"; |
674 clickHideFiltersDialog.style.visibility = "visible"; | 697 clickHideFiltersDialog.style.visibility = "visible"; |
(...skipping 27 matching lines...) Expand all Loading... | |
702 lastRightClickEventValid = false; | 725 lastRightClickEventValid = false; |
703 else | 726 else |
704 lastRightClickEvent = null; | 727 lastRightClickEvent = null; |
705 break; | 728 break; |
706 } | 729 } |
707 }); | 730 }); |
708 | 731 |
709 if (window == window.top) | 732 if (window == window.top) |
710 ext.backgroundPage.sendMessage({type: "report-html-page"}); | 733 ext.backgroundPage.sendMessage({type: "report-html-page"}); |
711 } | 734 } |
LEFT | RIGHT |