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

Delta Between Two Patch Sets: lib/content/snippets.js

Issue 29835561: Issue 6803 - Implement readd snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Add complementary remove-if-contains snippet Created July 23, 2018, 10:28 p.m.
Right Patch Set: Rename snippet to "readd" Created July 24, 2018, 3:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 * @param {...*} [args] The arguments to log. 160 * @param {...*} [args] The arguments to log.
161 */ 161 */
162 function trace(...args) 162 function trace(...args)
163 { 163 {
164 // We could simply use console.log here, but the goal is to demonstrate the 164 // We could simply use console.log here, but the goal is to demonstrate the
165 // usage of snippet dependencies. 165 // usage of snippet dependencies.
166 log(...args); 166 log(...args);
167 } 167 }
168 168
169 exports.trace = makeInjector(trace, log); 169 exports.trace = makeInjector(trace, log);
170
171 // This is an implementation of the uabinject-defuser technique used by uBlock
172 // Origin
173 // https://github.com/uBlockOrigin/uAssets/blob/c091f861b63cd2254b8e9e4628f6bdcd 89d43caa/filters/resources.txt#L640
174 function uabinjectDefuser()
175 {
176 window.trckd = true;
177 window.uabpdl = true;
178 window.uabInject = true;
179 window.uabDetect = true;
180 }
181
182 exports["uabinject-defuser"] = makeInjector(uabinjectDefuser);
170 183
171 /** 184 /**
172 * Hides any HTML element or one of its ancestors matching a CSS selector if 185 * Hides any HTML element or one of its ancestors matching a CSS selector if
173 * the text content of the element's shadow contains a given string. 186 * the text content of the element's shadow contains a given string.
174 * 187 *
175 * @param {string} search The string to look for in every HTML element's 188 * @param {string} search The string to look for in every HTML element's
176 * shadow. 189 * shadow.
177 * @param {string} selector The CSS selector that an HTML element must match 190 * @param {string} selector The CSS selector that an HTML element must match
178 * for it to be hidden. 191 * for it to be hidden.
179 */ 192 */
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 254
242 return root; 255 return root;
243 } 256 }
244 }); 257 });
245 } 258 }
246 259
247 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains, 260 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
248 hideElement); 261 hideElement);
249 262
250 /** 263 /**
251 * Untrashes a document by readding removed elements that match a CSS selector. 264 * Readds to the document any removed HTML elements that match a CSS selector.
252 * 265 *
253 * @param {string} selector The CSS selector that a removed element should 266 * @param {string} selector The CSS selector that a removed HTML element should
254 * match for it to be added back. 267 * match for it to be added back.
255 * @param {string?} [parentSelector] The CSS selector that a removed element's 268 * @param {string?} [parentSelector] The CSS selector that a removed HTML
256 * former parent should match for it to be added back. 269 * element's former parent should match for it to be added back.
257 * @param {string?} [ifContains] The string that should be found in the text 270 */
258 * content of the removed element for it to be added back. 271 function readd(selector, parentSelector = null)
259 * @param {string?} [unlessContains] The string that should not be found in the
260 * text content of the removed element for it to be added back.
261 */
262 function untrash(selector, parentSelector = null, ifContains = null,
263 unlessContains = null)
264 { 272 {
265 observe(document, {childList: true, subtree: true}, mutation => 273 observe(document, {childList: true, subtree: true}, mutation =>
266 { 274 {
267 if (mutation.removedNodes && 275 if (mutation.removedNodes &&
268 (!parentSelector || (mutation.target instanceof Element && 276 (!parentSelector || (mutation.target instanceof Element &&
269 mutation.target.matches(parentSelector)))) 277 mutation.target.matches(parentSelector))))
270 { 278 {
271 for (let node of mutation.removedNodes) 279 for (let node of mutation.removedNodes)
272 { 280 {
273 if (node instanceof HTMLElement && node.matches(selector) && 281 if (node instanceof HTMLElement && node.matches(selector))
274 (!ifContains || node.textContent.includes(ifContains)) &&
275 (!unlessContains || !node.textContent.includes(unlessContains)))
276 { 282 {
277 // We don't have the location of the element in its former parent, 283 // We don't have the location of the element in its former parent,
278 // but it's usually OK to just add it at the end. 284 // but it's usually OK to just add it at the end.
279 mutation.target.appendChild(node); 285 mutation.target.appendChild(node);
280 } 286 }
281 } 287 }
282 } 288 }
283 }); 289 });
284 } 290 }
285 291
286 exports.untrash = untrash; 292 exports.readd = readd;
287
288 /**
289 * Removes any HTML element from the document if the element's text content
290 * contains a given string.
291 *
292 * @param {string} search The string to look for in every HTML element.
293 * @param {string} [selector] The CSS selector that an HTML element must match
294 * for it to be removed from the document.
295 */
296 function removeIfContains(search, selector = "*")
297 {
298 observe(document, {childList: true, subtree: true}, mutation =>
299 {
300 for (let node of mutation.addedNodes || [])
301 {
302 if (node instanceof HTMLElement && node.matches(selector) &&
303 node.textContent.includes(search))
304 {
305 mutation.target.removeChild(node);
306 }
307 }
308 });
309 }
310
311 exports["remove-if-contains"] = removeIfContains;
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld