| 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 |
| 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 currentElement_boxShadow = null; | |
| 23 var currentElement_backgroundColor; | |
| 24 var clickHideFilters = null; | 22 var clickHideFilters = null; |
| 25 var highlightedElementsSelector = null; | 23 var highlightedElementsSelector = null; |
| 26 var highlightedElementsBoxShadows = null; | |
| 27 var highlightedElementsBGColors = null; | |
| 28 var clickHideFiltersDialog = null; | 24 var clickHideFiltersDialog = null; |
| 29 var lastRightClickEvent = null; | 25 var lastRightClickEvent = null; |
| 26 | |
| 27 function quote(value) | |
| 28 { | |
| 29 return '"' + value.replace(/(["\\])/g, "\\$1") + '"'; | |
| 30 } | |
| 31 | |
| 32 function supportsShadowRoot(element) | |
| 33 { | |
| 34 if (!("createShadowRoot" in element)) | |
| 35 return false; | |
| 36 | |
| 37 // There are some elements (e.g. <textarea>), which don't | |
| 38 // support author created shadow roots and throw an exception. | |
| 39 var clone = element.cloneNode(false); | |
| 40 try | |
| 41 { | |
| 42 clone.createShadowRoot(); | |
| 43 } | |
| 44 catch (e) | |
| 45 { | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 // There are some elements (e.g. <input>), which support | |
| 50 // author created shadow roots, but ignore insertion points. | |
| 51 var child = document.createTextNode(""); | |
| 52 clone.appendChild(child); | |
| 53 | |
| 54 var shadow = document.createElement("shadow"); | |
| 55 clone.shadowRoot.appendChild(shadow); | |
| 56 | |
| 57 return shadow.getDistributedNodes()[0] == child; | |
| 58 } | |
| 59 | |
| 60 function highlightElement(element, shadowColor, backgroundColor) | |
| 61 { | |
| 62 unhighlightElement(element); | |
| 63 | |
| 64 var originalBoxShadowPriority = element.style.getPropertyPriority("box-shadow" ); | |
| 65 var originalBackgroundColorPriority = element.style.getPropertyPriority("backg round-color"); | |
| 66 | |
| 67 var boxShadow = "inset 0px 0px 5px " + shadowColor; | |
| 68 | |
| 69 var highlightWithShadowDOM = function() | |
| 70 { | |
| 71 var style = document.createElement("style"); | |
| 72 style.textContent = ":host {" + | |
| 73 "box-shadow:" + boxShadow + " !important;" + | |
| 74 "background-color:" + backgroundColor + " !important;" + | |
| 75 "}"; | |
| 76 | |
| 77 var root = element.createShadowRoot(); | |
| 78 root.appendChild(document.createElement("shadow")); | |
| 79 root.appendChild(style); | |
| 80 | |
| 81 element._unhighlight = function() | |
| 82 { | |
| 83 root.removeChild(style); | |
| 84 }; | |
| 85 }; | |
| 86 | |
| 87 var highlightWithStyleAttribute = function() | |
| 88 { | |
| 89 var originalBoxShadow = element.style.getPropertyValue("box-shadow"); | |
| 90 var originalBackgroundColor = element.style.getPropertyValue("background-col or"); | |
| 91 | |
| 92 element.style.setProperty("box-shadow", boxShadow, "important"); | |
| 93 element.style.setProperty("background-color", backgroundColor, "important"); | |
| 94 | |
| 95 element._unhighlight = function() | |
| 96 { | |
| 97 this.style.removeProperty("box-shadow"); | |
| 98 this.style.setProperty( | |
| 99 "box-shadow", | |
| 100 originalBoxShadow, | |
| 101 originalBoxShadowPriority | |
| 102 ); | |
| 103 | |
| 104 this.style.removeProperty("background-color"); | |
| 105 this.style.setProperty( | |
| 106 "background-color", | |
| 107 originalBackgroundColor, | |
| 108 originalBackgroundColorPriority | |
| 109 ); | |
| 110 }; | |
| 111 }; | |
| 112 | |
| 113 // Use shadow DOM if posibble to avoid side effects when the | |
| 114 // web page updates style while highlighted. However, if the | |
| 115 // element has important styles we can't override them with shadow DOM. | |
| 116 if (supportsShadowRoot(element) && originalBoxShadowPriority != "importa nt" && | |
| 117 originalBackgroundColorPriority != "importa nt") | |
| 118 highlightWithShadowDOM(); | |
| 119 else | |
| 120 highlightWithStyleAttribute(); | |
| 121 } | |
| 122 | |
| 123 | |
| 124 function unhighlightElement(element) | |
| 125 { | |
| 126 if ("_unhighlight" in element) | |
| 127 { | |
| 128 element._unhighlight(); | |
| 129 delete element._unhighlight; | |
| 130 } | |
| 131 } | |
| 30 | 132 |
| 31 // Highlight elements according to selector string. This would include | 133 // Highlight elements according to selector string. This would include |
| 32 // all elements that would be affected by proposed filters. | 134 // all elements that would be affected by proposed filters. |
| 33 function highlightElements(selectorString) { | 135 function highlightElements(selectorString) { |
| 34 if(highlightedElementsSelector) | 136 unhighlightElements(); |
| 35 unhighlightElements(); | |
| 36 | 137 |
| 37 var highlightedElements = document.querySelectorAll(selectorString); | 138 var highlightedElements = document.querySelectorAll(selectorString); |
| 38 highlightedElementsSelector = selectorString; | 139 highlightedElementsSelector = selectorString; |
| 39 highlightedElementsBoxShadows = new Array(); | 140 |
| 40 highlightedElementsBGColors = new Array(); | 141 for(var i = 0; i < highlightedElements.length; i++) |
| 41 | 142 highlightElement(highlightedElements[i], "#fd6738", "#f6e1e5"); |
| 42 for(var i = 0; i < highlightedElements.length; i++) { | |
| 43 highlightedElementsBoxShadows[i] = highlightedElements[i].style.getPropertyV alue("-webkit-box-shadow"); | |
| 44 highlightedElementsBGColors[i] = highlightedElements[i].style.backgroundColo r; | |
| 45 highlightedElements[i].style.setProperty("-webkit-box-shadow", "inset 0px 0p x 5px #fd6738"); | |
| 46 highlightedElements[i].style.backgroundColor = "#f6e1e5"; | |
| 47 } | |
| 48 } | 143 } |
| 49 | 144 |
| 50 // Unhighlight all elements, including those that would be affected by | 145 // Unhighlight all elements, including those that would be affected by |
| 51 // the proposed filters | 146 // the proposed filters |
| 52 function unhighlightElements() { | 147 function unhighlightElements() { |
| 53 if(highlightedElementsSelector == null) | 148 if (highlightedElementsSelector) |
| 54 return; | 149 { |
| 55 var highlightedElements = document.querySelectorAll(highlightedElementsSelecto r); | 150 Array.prototype.forEach.call( |
| 56 for(var i = 0; i < highlightedElements.length; i++) { | 151 document.querySelectorAll(highlightedElementsSelector), |
| 57 highlightedElements[i].style.setProperty("-webkit-box-shadow", highlightedEl ementsBoxShadows[i]); | 152 unhighlightElement |
| 58 highlightedElements[i].style.backgroundColor = highlightedElementsBGColors[i ]; | 153 ); |
| 59 } | 154 |
| 60 highlightedElementsSelector = null; | 155 highlightedElementsSelector = null; |
| 156 } | |
| 61 } | 157 } |
| 62 | 158 |
| 63 // Gets the absolute position of an element by walking up the DOM tree, | 159 // Gets the absolute position of an element by walking up the DOM tree, |
| 64 // adding up offsets. | 160 // adding up offsets. |
| 65 // I hope there's a better way because it just seems absolutely stupid | 161 // I hope there's a better way because it just seems absolutely stupid |
| 66 // that the DOM wouldn't have a direct way to get this, given that it | 162 // that the DOM wouldn't have a direct way to get this, given that it |
| 67 // has hundreds and hundreds of other methods that do random junk. | 163 // has hundreds and hundreds of other methods that do random junk. |
| 68 function getAbsolutePosition(elt) { | 164 function getAbsolutePosition(elt) { |
| 69 var l = 0; | 165 var l = 0; |
| 70 var t = 0; | 166 var t = 0; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 clickHide_deactivate(); | 207 clickHide_deactivate(); |
| 112 currentElement = savedElement; | 208 currentElement = savedElement; |
| 113 } | 209 } |
| 114 | 210 |
| 115 clickHide_filters = filters; | 211 clickHide_filters = filters; |
| 116 | 212 |
| 117 clickHideFiltersDialog = document.createElement("iframe"); | 213 clickHideFiltersDialog = document.createElement("iframe"); |
| 118 clickHideFiltersDialog.src = ext.getURL("block.html"); | 214 clickHideFiltersDialog.src = ext.getURL("block.html"); |
| 119 clickHideFiltersDialog.setAttribute("style", "position: fixed !important; visi bility: hidden; display: block !important; border: 0px !important;"); | 215 clickHideFiltersDialog.setAttribute("style", "position: fixed !important; visi bility: hidden; display: block !important; border: 0px !important;"); |
| 120 clickHideFiltersDialog.style.WebkitBoxShadow = "5px 5px 20px rgba(0,0,0,0.5)"; | 216 clickHideFiltersDialog.style.WebkitBoxShadow = "5px 5px 20px rgba(0,0,0,0.5)"; |
| 121 clickHideFiltersDialog.style.zIndex = 99999; | 217 clickHideFiltersDialog.style.zIndex = 0x7FFFFFFF; |
| 122 | 218 |
| 123 // Position in upper-left all the time | 219 // Position in upper-left all the time |
| 124 clickHideFiltersDialog.style.left = "50px"; | 220 clickHideFiltersDialog.style.left = "50px"; |
| 125 clickHideFiltersDialog.style.top = "50px"; | 221 clickHideFiltersDialog.style.top = "50px"; |
| 126 | 222 |
| 127 // Make dialog partly transparent when mouse isn't over it so user has a bette r | 223 // Make dialog partly transparent when mouse isn't over it so user has a bette r |
| 128 // view of what's going to be blocked | 224 // view of what's going to be blocked |
| 129 clickHideFiltersDialog.onmouseout = function() | 225 clickHideFiltersDialog.onmouseout = function() |
| 130 { | 226 { |
| 131 if (clickHideFiltersDialog) | 227 if (clickHideFiltersDialog) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 { | 272 { |
| 177 if (clickHideFiltersDialog) | 273 if (clickHideFiltersDialog) |
| 178 { | 274 { |
| 179 document.body.removeChild(clickHideFiltersDialog); | 275 document.body.removeChild(clickHideFiltersDialog); |
| 180 clickHideFiltersDialog = null; | 276 clickHideFiltersDialog = null; |
| 181 } | 277 } |
| 182 | 278 |
| 183 if(currentElement) { | 279 if(currentElement) { |
| 184 currentElement.removeEventListener("contextmenu", clickHide_elementClickHand ler, false); | 280 currentElement.removeEventListener("contextmenu", clickHide_elementClickHand ler, false); |
| 185 unhighlightElements(); | 281 unhighlightElements(); |
| 186 currentElement.style.setProperty("-webkit-box-shadow", currentElement_boxSha dow); | 282 unhighlightElement(currentElement); |
| 187 currentElement.style.backgroundColor = currentElement_backgroundColor; | |
| 188 currentElement = null; | 283 currentElement = null; |
| 189 clickHideFilters = null; | 284 clickHideFilters = null; |
| 190 } | 285 } |
| 191 unhighlightElements(); | 286 unhighlightElements(); |
| 192 | 287 |
| 193 clickHide_activated = false; | 288 clickHide_activated = false; |
| 194 clickHide_filters = null; | 289 clickHide_filters = null; |
| 195 if(!document) | 290 if(!document) |
| 196 return; // This can happen inside a nuked iframe...I think | 291 return; // This can happen inside a nuked iframe...I think |
| 197 document.removeEventListener("mouseover", clickHide_mouseOver, false); | 292 document.removeEventListener("mouseover", clickHide_mouseOver, false); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 212 clickHide_mouseClick(ev); | 307 clickHide_mouseClick(ev); |
| 213 } | 308 } |
| 214 | 309 |
| 215 // Hovering over an element so highlight it | 310 // Hovering over an element so highlight it |
| 216 function clickHide_mouseOver(e) | 311 function clickHide_mouseOver(e) |
| 217 { | 312 { |
| 218 if (clickHide_activated == false) | 313 if (clickHide_activated == false) |
| 219 return; | 314 return; |
| 220 | 315 |
| 221 var target = e.target; | 316 var target = e.target; |
| 222 while (target.parentNode && target.attributes.length == 0) | 317 while (target.parentNode && !(target.id || target.className || target.src || / :.+:/.test(target.getAttribute("style")))) |
| 223 target = target.parentNode; | 318 target = target.parentNode; |
| 224 if (target == document.documentElement || target == document.body) | 319 if (target == document.documentElement || target == document.body) |
| 225 target = null; | 320 target = null; |
| 226 | 321 |
| 227 if (target && target instanceof HTMLElement) | 322 if (target && target instanceof HTMLElement) |
| 228 { | 323 { |
| 229 currentElement = target; | 324 currentElement = target; |
| 230 currentElement_boxShadow = target.style.getPropertyValue("-webkit-box-shadow "); | 325 |
| 231 currentElement_backgroundColor = target.style.backgroundColor; | 326 highlightElement(target, "#d6d84b", "#f8fa47"); |
| 232 target.style.setProperty("-webkit-box-shadow", "inset 0px 0px 5px #d6d84b"); | |
| 233 target.style.backgroundColor = "#f8fa47"; | |
| 234 | |
| 235 target.addEventListener("contextmenu", clickHide_elementClickHandler, false) ; | 327 target.addEventListener("contextmenu", clickHide_elementClickHandler, false) ; |
| 236 } | 328 } |
| 237 } | 329 } |
| 238 | 330 |
| 239 // No longer hovering over this element so unhighlight it | 331 // No longer hovering over this element so unhighlight it |
| 240 function clickHide_mouseOut(e) | 332 function clickHide_mouseOut(e) |
| 241 { | 333 { |
| 242 if (!clickHide_activated || !currentElement) | 334 if (!clickHide_activated || !currentElement) |
| 243 return; | 335 return; |
| 244 | 336 |
| 245 currentElement.style.setProperty("-webkit-box-shadow", currentElement_boxShado w); | 337 unhighlightElement(currentElement); |
| 246 currentElement.style.backgroundColor = currentElement_backgroundColor; | |
| 247 | |
| 248 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, false); | 338 currentElement.removeEventListener("contextmenu", clickHide_elementClickHandle r, false); |
| 249 } | 339 } |
| 250 | 340 |
| 251 // Selects the currently hovered-over filter or cancels selection | 341 // Selects the currently hovered-over filter or cancels selection |
| 252 function clickHide_keyDown(e) | 342 function clickHide_keyDown(e) |
| 253 { | 343 { |
| 254 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /) | 344 if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 13 /*DOM_VK_RETURN* /) |
| 255 clickHide_mouseClick(e); | 345 clickHide_mouseClick(e); |
| 256 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/) | 346 else if (!e.ctrlKey && !e.altKey && !e.shiftKey && e.keyCode == 27 /*DOM_VK_ES CAPE*/) |
| 257 { | 347 { |
| 258 clickHide_deactivate(); | 348 ext.backgroundPage.sendMessage( |
| 349 { | |
| 350 type: "forward", | |
| 351 payload: | |
| 352 { | |
| 353 type: "clickhide-deactivate" | |
| 354 } | |
| 355 }); | |
| 259 e.preventDefault(); | 356 e.preventDefault(); |
| 260 e.stopPropagation(); | 357 e.stopPropagation(); |
| 261 } | 358 } |
| 262 } | 359 } |
| 263 | 360 |
| 264 // When the user clicks, the currentElement is the one we want. | 361 // When the user clicks, the currentElement is the one we want. |
| 265 // We should have ABP rules ready for when the | 362 // We should have ABP rules ready for when the |
| 266 // popup asks for them. | 363 // popup asks for them. |
| 267 function clickHide_mouseClick(e) | 364 function clickHide_mouseClick(e) |
| 268 { | 365 { |
| 269 if (!currentElement || !clickHide_activated) | 366 if (!currentElement || !clickHide_activated) |
| 270 return; | 367 return; |
| 271 | 368 |
| 272 var elt = currentElement; | 369 var elt = currentElement; |
| 273 var url = null; | 370 var url = null; |
| 274 if (currentElement.className && currentElement.className == "__adblockplus__ov erlay") | 371 if (currentElement.className && currentElement.className == "__adblockplus__ov erlay") |
| 275 { | 372 { |
| 276 elt = currentElement.prisoner; | 373 elt = currentElement.prisoner; |
| 277 url = currentElement.prisonerURL; | 374 url = currentElement.prisonerURL; |
| 278 } | 375 } |
| 279 else if (elt.src) | 376 else if (elt.src) |
| 280 url = elt.src; | 377 url = elt.src; |
| 281 | |
| 282 // Only normalize when the element contains a URL (issue 328.) | |
| 283 // The URL is not always normalized, so do it here | |
| 284 if (url) | |
| 285 url = normalizeURL(relativeToAbsoluteUrl(url)); | |
| 286 | 378 |
| 287 // Construct filters. The popup will retrieve these. | 379 // Construct filters. The popup will retrieve these. |
| 288 // Only one ID | 380 // Only one ID |
| 289 var elementId = elt.id ? elt.id.split(' ').join('') : null; | 381 var elementId = elt.id ? elt.id.split(' ').join('') : null; |
| 290 // Can have multiple classes, and there might be extraneous whitespace | 382 // Can have multiple classes, and there might be extraneous whitespace |
| 291 var elementClasses = null; | 383 var elementClasses = null; |
| 292 if (elt.className) | 384 if (elt.className) |
| 293 elementClasses = elt.className.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '' ).split(' '); | 385 elementClasses = elt.className.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '' ).split(' '); |
| 294 | 386 |
| 295 clickHideFilters = new Array(); | 387 clickHideFilters = new Array(); |
| 296 selectorList = new Array(); | 388 selectorList = new Array(); |
| 297 | 389 |
| 298 var addSelector = function(selector) | 390 var addSelector = function(selector) |
| 299 { | 391 { |
| 300 clickHideFilters.push(document.domain + "##" + selector); | 392 clickHideFilters.push(document.domain + "##" + selector); |
| 301 selectorList.push(selector); | 393 selectorList.push(selector); |
| 302 }; | 394 }; |
| 303 | 395 |
| 304 if (elementId) | 396 if (elementId) |
| 305 addSelector("#" + elementId); | 397 addSelector("#" + elementId); |
| 306 | 398 |
| 307 if (elt.classList.length > 0) | 399 if (elementClasses && elementClasses.length > 0) |
| 308 { | 400 { |
| 309 var selector = ""; | 401 var selector = ""; |
| 310 | 402 |
| 311 for (var i = 0; i < elt.classList.length; i++) | 403 for (var i = 0; i < elt.classList.length; i++) |
|
Thomas Greiner
2014/11/04 09:53:21
It's certainly not wrong to use elt.classList here
Sebastian Noack
2014/11/04 14:51:07
The patch has already been merged, but I might fil
Thomas Greiner
2014/11/04 15:15:53
That's totally fine with me. Looks like the origin
Sebastian Noack
2014/11/04 15:44:54
For reference, https://issues.adblockplus.org/tick
| |
| 312 selector += "." + elt.classList[i].replace(/([^\w-])/, "\\$1"); | 404 selector += "." + elt.classList[i].replace(/([^\w-])/, "\\$1"); |
|
Wladimir Palant
2014/11/03 18:11:33
Somehow I feel that changing Array.map() into a lo
| |
| 313 | 405 |
| 314 addSelector(selector); | 406 addSelector(selector); |
| 315 } | 407 } |
| 316 | 408 |
| 317 if (url) | 409 if (url) |
| 318 { | 410 { |
| 319 clickHideFilters.push(relativeToAbsoluteUrl(url)); | 411 clickHideFilters.push(url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||")); |
| 320 selectorList.push(elt.localName + '[src="' + url + '"]'); | 412 |
| 413 var src = elt.getAttribute("src"); | |
| 414 if (src) | |
| 415 selectorList.push('[src=' + quote(src) + ']'); | |
| 321 } | 416 } |
| 322 | 417 |
| 323 // restore the original style, before generating the fallback filter that | 418 // restore the original style, before generating the fallback filter that |
| 324 // will include the style, and to prevent highlightElements from saving those | 419 // will include the style, and to prevent highlightElements from saving those |
| 325 currentElement.style.setProperty("-webkit-box-shadow", currentElement_boxShado w); | 420 unhighlightElement(currentElement); |
| 326 currentElement.style.backgroundColor = currentElement_backgroundColor; | 421 |
| 327 | 422 // as last resort, create a filter based on inline styles |
| 328 // as last resort, create a filter based on tag's name | |
| 329 // and all of its attributes, if everything else fails | |
| 330 if (clickHideFilters.length == 0) | 423 if (clickHideFilters.length == 0) |
| 331 { | 424 { |
| 332 var selector = elt.localName; | 425 var style = elt.getAttribute("style"); |
| 333 | 426 if (style) |
| 334 for (var i = 0; i < elt.attributes.length; i++) | 427 addSelector(elt.localName + '[style=' + quote(style) + ']'); |
| 335 selector += "[" + elt.attributes[i].name + '="' + elt.attributes[i].value. replace('"', '\\"') + '"]'; | |
| 336 | |
| 337 addSelector(selector); | |
| 338 } | 428 } |
| 339 | 429 |
| 340 // Show popup | 430 // Show popup |
| 341 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters); | 431 clickHide_showDialog(e.clientX, e.clientY, clickHideFilters); |
| 342 | 432 |
| 343 // Highlight the elements specified by selector in yellow | 433 // Highlight the elements specified by selector in yellow |
| 344 highlightElements(selectorList.join(",")); | 434 highlightElements(selectorList.join(",")); |
| 345 // Now, actually highlight the element the user clicked on in red | 435 // Now, actually highlight the element the user clicked on in red |
| 346 currentElement.style.setProperty("-webkit-box-shadow", "inset 0px 0px 5px #fd1 708"); | 436 highlightElement(currentElement, "#fd1708", "#f6a1b5"); |
| 347 currentElement.style.backgroundColor = "#f6a1b5"; | |
| 348 | 437 |
| 349 // Make sure the browser doesn't handle this click | 438 // Make sure the browser doesn't handle this click |
| 350 e.preventDefault(); | 439 e.preventDefault(); |
| 351 e.stopPropagation(); | 440 e.stopPropagation(); |
| 352 } | 441 } |
| 353 | 442 |
| 354 // Extracts source URL from an IMG, OBJECT, EMBED, or IFRAME | 443 // Extracts source URL from an IMG, OBJECT, EMBED, or IFRAME |
| 355 function getElementURL(elt) { | 444 function getElementURL(elt) { |
| 356 // Check children of object nodes for "param" nodes with name="movie" that spe cify a URL | 445 // Check children of object nodes for "param" nodes with name="movie" that spe cify a URL |
| 357 // in value attribute | 446 // in value attribute |
| 358 var url; | 447 var url; |
| 359 if(elt.localName.toUpperCase() == "OBJECT" && !(url = elt.getAttribute("data") )) { | 448 if(elt.localName.toUpperCase() == "OBJECT" && !(url = elt.getAttribute("data") )) { |
| 360 // No data attribute, look in PARAM child tags for a URL for the swf file | 449 // No data attribute, look in PARAM child tags for a URL for the swf file |
| 361 var params = elt.querySelectorAll("param[name=\"movie\"]"); | 450 var params = elt.querySelectorAll("param[name=\"movie\"]"); |
| 362 // This OBJECT could contain an EMBED we already nuked, in which case there' s no URL | 451 // This OBJECT could contain an EMBED we already nuked, in which case there' s no URL |
| 363 if(params[0]) | 452 if(params[0]) |
| 364 url = params[0].getAttribute("value"); | 453 url = params[0].getAttribute("value"); |
| 365 else { | 454 else { |
| 366 params = elt.querySelectorAll("param[name=\"src\"]"); | 455 params = elt.querySelectorAll("param[name=\"src\"]"); |
| 367 if(params[0]) | 456 if(params[0]) |
| 368 url = params[0].getAttribute("value"); | 457 url = params[0].getAttribute("value"); |
| 369 } | 458 } |
| 459 | |
| 460 if (url) | |
| 461 url = resolveURL(url); | |
| 370 } else if(!url) { | 462 } else if(!url) { |
| 371 url = elt.getAttribute("src") || elt.getAttribute("href"); | 463 url = elt.src || elt.href; |
| 372 } | 464 } |
| 373 return url; | 465 return url; |
| 374 } | 466 } |
| 375 | 467 |
| 376 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js | 468 // This function Copyright (c) 2008 Jeni Tennison, from jquery.uri.js |
| 377 // and licensed under the MIT license. See jquery-*.min.js for details. | 469 // and licensed under the MIT license. See jquery-*.min.js for details. |
| 378 function removeDotSegments(u) { | 470 function removeDotSegments(u) { |
| 379 var r = '', m = []; | 471 var r = '', m = []; |
| 380 if (/\./.test(u)) { | 472 if (/\./.test(u)) { |
| 381 while (u !== undefined && u !== '') { | 473 while (u !== undefined && u !== '') { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 395 u = m[2]; | 487 u = m[2]; |
| 396 r = r + m[1]; | 488 r = r + m[1]; |
| 397 } | 489 } |
| 398 } | 490 } |
| 399 return r; | 491 return r; |
| 400 } else { | 492 } else { |
| 401 return u; | 493 return u; |
| 402 } | 494 } |
| 403 } | 495 } |
| 404 | 496 |
| 405 // Does some degree of URL normalization | 497 if (document instanceof HTMLDocument) |
| 406 function normalizeURL(url) | |
| 407 { | |
| 408 var components = url.match(/(.+:\/\/.+?)\/(.*)/); | |
| 409 if(!components) | |
| 410 return url; | |
| 411 var newPath = removeDotSegments(components[2]); | |
| 412 if(newPath.length == 0) | |
| 413 return components[1]; | |
| 414 if(newPath[0] != '/') | |
| 415 newPath = '/' + newPath; | |
| 416 return components[1] + newPath; | |
| 417 } | |
| 418 | |
| 419 // Content scripts are apparently invoked on non-HTML documents, so we have to | |
| 420 // check for that before doing stuff. |document instanceof HTMLDocument| check | |
| 421 // will fail on some sites like planet.mozilla.org because WebKit creates | |
| 422 // Document instances for XHTML documents, have to test the root element. | |
| 423 if (document.documentElement instanceof HTMLElement) | |
| 424 { | 498 { |
| 425 // Use a contextmenu handler to save the last element the user right-clicked o n. | 499 // Use a contextmenu handler to save the last element the user right-clicked o n. |
| 426 // To make things easier, we actually save the DOM event. | 500 // To make things easier, we actually save the DOM event. |
| 427 // We have to do this because the contextMenu API only provides a URL, not the actual | 501 // We have to do this because the contextMenu API only provides a URL, not the actual |
| 428 // DOM element. | 502 // DOM element. |
| 429 document.addEventListener('contextmenu', function(e) { | 503 document.addEventListener('contextmenu', function(e) { |
| 430 lastRightClickEvent = e; | 504 lastRightClickEvent = e; |
| 431 }, false); | 505 }, false); |
| 432 | 506 |
| 433 document.addEventListener("click", function(event) | 507 document.addEventListener("click", function(event) |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 537 if(msg.filter === url) | 611 if(msg.filter === url) |
| 538 { | 612 { |
| 539 // This request would have come from the chrome.contextMenu handler, s o we | 613 // This request would have come from the chrome.contextMenu handler, s o we |
| 540 // simulate the user having chosen the element to get rid of via the u sual means. | 614 // simulate the user having chosen the element to get rid of via the u sual means. |
| 541 clickHide_activated = true; | 615 clickHide_activated = true; |
| 542 // FIXME: clickHideFilters is erased in clickHide_mouseClick anyway, s o why set it? | 616 // FIXME: clickHideFilters is erased in clickHide_mouseClick anyway, s o why set it? |
| 543 clickHideFilters = [msg.filter]; | 617 clickHideFilters = [msg.filter]; |
| 544 // Coerce red highlighted overlay on top of element to remove. | 618 // Coerce red highlighted overlay on top of element to remove. |
| 545 // TODO: Wow, the design of the clickHide stuff is really dumb - gotta fix it sometime | 619 // TODO: Wow, the design of the clickHide stuff is really dumb - gotta fix it sometime |
| 546 currentElement = addElementOverlay(target); | 620 currentElement = addElementOverlay(target); |
| 547 currentElement_backgroundColor = target.style.backgroundColor; | |
| 548 // clickHide_mouseOver(lastRightClickEvent); | 621 // clickHide_mouseOver(lastRightClickEvent); |
| 549 clickHide_mouseClick(lastRightClickEvent); | 622 clickHide_mouseClick(lastRightClickEvent); |
| 550 } | 623 } |
| 551 else | 624 else |
| 552 console.log("clickhide-new-filter: URLs don't match. Couldn't find tha t element.", request.filter, url, lastRightClickEvent.target.src); | 625 console.log("clickhide-new-filter: URLs don't match. Couldn't find tha t element.", msg.filter, url, lastRightClickEvent.target.src); |
| 553 break; | 626 break; |
| 554 case "clickhide-init": | 627 case "clickhide-init": |
| 555 if (clickHideFiltersDialog) | 628 if (clickHideFiltersDialog) |
| 556 { | 629 { |
| 557 sendResponse({filters: clickHide_filters}); | 630 sendResponse({filters: clickHide_filters}); |
| 558 | 631 |
| 559 clickHideFiltersDialog.style.width = msg.width + "px"; | 632 clickHideFiltersDialog.style.width = msg.width + "px"; |
| 560 clickHideFiltersDialog.style.height = msg.height + "px"; | 633 clickHideFiltersDialog.style.height = msg.height + "px"; |
| 561 clickHideFiltersDialog.style.visibility = "visible"; | 634 clickHideFiltersDialog.style.visibility = "visible"; |
| 562 } | 635 } |
| 563 break; | 636 break; |
| 564 case "clickhide-move": | 637 case "clickhide-move": |
| 565 if (clickHideFiltersDialog) | 638 if (clickHideFiltersDialog) |
| 566 { | 639 { |
| 567 clickHideFiltersDialog.style.left = (parseInt(clickHideFiltersDialog.s tyle.left, 10) + request.x) + "px"; | 640 clickHideFiltersDialog.style.left = (parseInt(clickHideFiltersDialog.s tyle.left, 10) + msg.x) + "px"; |
| 568 clickHideFiltersDialog.style.top = (parseInt(clickHideFiltersDialog.st yle.top, 10) + request.y) + "px"; | 641 clickHideFiltersDialog.style.top = (parseInt(clickHideFiltersDialog.st yle.top, 10) + msg.y) + "px"; |
| 569 } | 642 } |
| 570 break; | 643 break; |
| 571 case "clickhide-close": | 644 case "clickhide-close": |
| 572 if (clickHideFiltersDialog) | 645 if (clickHideFiltersDialog) |
| 573 { | 646 { |
| 574 // Explicitly get rid of currentElement | 647 // Explicitly get rid of currentElement |
| 575 if (msg.remove && currentElement && currentElement.parentNode) | 648 if (msg.remove && currentElement && currentElement.parentNode) |
| 576 currentElement.parentNode.removeChild(currentElement); | 649 currentElement.parentNode.removeChild(currentElement); |
| 577 | |
| 578 clickHide_deactivate(); | |
| 579 } | 650 } |
| 651 clickHide_deactivate(); | |
| 580 break; | 652 break; |
| 581 default: | 653 default: |
| 582 sendResponse({}); | 654 sendResponse({}); |
| 583 break; | 655 break; |
| 584 } | 656 } |
| 585 }); | 657 }); |
| 586 } | 658 |
| 659 if (window == window.top) | |
| 660 ext.backgroundPage.sendMessage({type: "report-html-page"}); | |
| 661 } | |
| LEFT | RIGHT |