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

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

Issue 29886700: Issue 6969 - Implement abort-on-property-read snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Addressed all review but the snippets parameters. Created Sept. 25, 2018, 4:28 a.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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 { 429 {
430 for (let i = 0; i < times; i++) 430 for (let i = 0; i < times; i++)
431 object + ""; 431 object + "";
432 432
433 if (typeof dir == "function") 433 if (typeof dir == "function")
434 dir.call(this, object); 434 dir.call(this, object);
435 }; 435 };
436 } 436 }
437 437
438 exports["dir-string"] = makeInjector(dirString); 438 exports["dir-string"] = makeInjector(dirString);
439
440 function randomId()
441 {
442 // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
443 // 60466176 is 36^5
444 // 2176782336 - 60466176 = 2116316160. This ensure always 6 chars
445 // for when Math.random() returns 0.0
446 return (Math.floor(Math.random() * 2116316160 + 60466176)).toString(36);
Manish Jethani 2018/09/25 12:25:22 Nit: The parentheses around `Math.foor(...)` are u
hub 2018/09/25 15:40:43 Done.
447 }
448
449 /**
450 * Will patch a property on the window object to abort when read.
451 * It will intercept the onerror callback and block it if tagged.
452 *
453 * @todo handle properties of properties.
454 *
455 * @param {string} prop the name of the property.
456 */
457 function abortOnPropertyRead(...props)
458 {
459 if (props.length == 0)
460 return;
461
462 let magic = randomId();
463
464 let abort = function()
465 {
466 throw new ReferenceError(magic);
467 };
468
469 let {onerror} = window;
470 window.onerror = (message, ...rest) =>
471 {
472 if (typeof message == "string" && message.includes(magic))
473 return true;
474 if (onerror && typeof onerro == "function")
475 return onerror(this, message, ...rest);
476 };
477
478 let patch = function(o, p)
479 {
480 // simple property
481 let d = Object.getOwnPropertyDescriptor(o, p);
482 if (!d || d.get != abort)
483 Object.defineProperty(o, p, {get: abort, set() {}});
484 };
485 for (let prop of props)
486 patch(window, prop);
487 }
488
489 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, randomId);
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