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

Side by Side Diff: lib/content/snippets.js

Issue 29835561: Issue 6803 - Implement readd snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add complementary remove-if-contains snippet Created July 23, 2018, 10:28 p.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 | no next file » | 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 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (element.style.getPropertyValue("display") != "none" || 113 if (element.style.getPropertyValue("display") != "none" ||
114 element.style.getPropertyPriority("display") != "important") 114 element.style.getPropertyPriority("display") != "important")
115 { 115 {
116 element.style.setProperty("display", "none", "important"); 116 element.style.setProperty("display", "none", "important");
117 } 117 }
118 }) 118 })
119 .observe(element, {attributes: true, attributeFilter: ["style"]}); 119 .observe(element, {attributes: true, attributeFilter: ["style"]});
120 } 120 }
121 121
122 /** 122 /**
123 * Observes changes to a DOM node using a <code>MutationObserver</code>.
124 *
125 * @param {Node} target The DOM node to observe for changes.
126 * @param {MutationObserverInit?} [options] Options that describe what DOM
127 * mutations should be reported to the callback.
128 * @param {function} callback A function that will be called on each DOM
129 * mutation, taking a <code>MutationRecord</code> as its parameter.
130 */
131 function observe(target, options, callback)
132 {
133 new MutationObserver(mutations =>
134 {
135 for (let mutation of mutations)
136 callback(mutation);
137 })
138 .observe(target, options);
139 }
140
141 /**
123 * Logs its arguments to the console. This may be used for testing and 142 * Logs its arguments to the console. This may be used for testing and
124 * debugging. 143 * debugging.
125 * 144 *
126 * @param {...*} [args] The arguments to log. 145 * @param {...*} [args] The arguments to log.
127 */ 146 */
128 function log(...args) 147 function log(...args)
129 { 148 {
130 console.log(...args); 149 console.log(...args);
131 } 150 }
132 151
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // will be removed. 239 // will be removed.
221 shadows.set(observer, {host: this, root}); 240 shadows.set(observer, {host: this, root});
222 241
223 return root; 242 return root;
224 } 243 }
225 }); 244 });
226 } 245 }
227 246
228 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains, 247 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
229 hideElement); 248 hideElement);
249
250 /**
251 * Untrashes a document by readding removed elements that match a CSS selector.
252 *
253 * @param {string} selector The CSS selector that a removed element should
254 * match for it to be added back.
255 * @param {string?} [parentSelector] The CSS selector that a removed element's
256 * former parent should match for it to be added back.
257 * @param {string?} [ifContains] The string that should be found in the text
258 * content of the removed element for it to be added back.
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 {
265 observe(document, {childList: true, subtree: true}, mutation =>
266 {
267 if (mutation.removedNodes &&
268 (!parentSelector || (mutation.target instanceof Element &&
269 mutation.target.matches(parentSelector))))
270 {
271 for (let node of mutation.removedNodes)
272 {
273 if (node instanceof HTMLElement && node.matches(selector) &&
274 (!ifContains || node.textContent.includes(ifContains)) &&
275 (!unlessContains || !node.textContent.includes(unlessContains)))
276 {
277 // 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.
279 mutation.target.appendChild(node);
280 }
281 }
282 }
283 });
284 }
285
286 exports.untrash = untrash;
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;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld