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

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

Issue 29828610: Issue 6791 - Implement abort-current-inline-script snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Refactor Created July 13, 2018, 12:12 p.m.
Right Patch Set: Rebase Created July 13, 2018, 1 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 function getMagic() 102 function getMagic()
103 { 103 {
104 return String.fromCharCode(Date.now() % 26 + 97) + 104 return String.fromCharCode(Date.now() % 26 + 97) +
105 Math.floor(Math.random() * 982451653 + 982451653).toString(36); 105 Math.floor(Math.random() * 982451653 + 982451653).toString(36);
106 } 106 }
107 107
108 function suppressMagicError(magic) 108 function suppressMagicError(magic)
109 { 109 {
110 let {onerror} = window; 110 let {onerror} = window;
111 window.onerror = function(message, ...rest) 111 window.onerror = function(message, ...rest)
kzar 2018/07/16 16:33:15 The content script part of our snippet already run
112 { 112 {
113 if (typeof message == "string" && message.includes(magic)) 113 if (typeof message == "string" && message.includes(magic))
114 return true; 114 return true;
115 115
116 if (typeof onerror instanceof Function) 116 if (typeof onerror instanceof Function)
117 return onerror.call(this, message, ...rest); 117 return onerror.call(this, message, ...rest);
118 }; 118 };
119 } 119 }
120 120
121 function log(...args) 121 function log(...args)
122 { 122 {
123 console.log(...args); 123 console.log(...args);
124 } 124 }
125 125
126 exports.log = log; 126 exports.log = log;
127 127
128 // This is an implementation of the uabinject-defuser technique used by uBlock
129 // Origin
130 // https://github.com/uBlockOrigin/uAssets/blob/c091f861b63cd2254b8e9e4628f6bdcd 89d43caa/filters/resources.txt#L640
128 function uabinjectDefuser() 131 function uabinjectDefuser()
129 { 132 {
130 window.trckd = true; 133 window.trckd = true;
131 window.uabpdl = true; 134 window.uabpdl = true;
132 window.uabInject = true; 135 window.uabInject = true;
133 window.uabDetect = true; 136 window.uabDetect = true;
134 } 137 }
135 138
136 exports["uabinject-defuser"] = makeInjector(uabinjectDefuser); 139 exports["uabinject-defuser"] = makeInjector(uabinjectDefuser);
137 140
141 // This is an implementation of the abort-current-inline-script technique used
142 // by uBlock Origin
143 // https://github.com/uBlockOrigin/uAssets/blob/68cf8a364fb55deb96264c4e546b58f4 c851d782/filters/resources.txt#L1943
138 function abortCurrentInlineScript(target, needle) 144 function abortCurrentInlineScript(target, needle)
kzar 2018/07/16 16:33:15 Could you give me an example of `target` and `need
139 { 145 {
140 if (!target) 146 if (!target)
141 return; 147 return;
142 148
143 let re = null; 149 let re = null;
144 150
145 if (needle) 151 if (needle)
146 { 152 {
147 re = new RegExp(/^\/.+\/$/.test(needle) ? 153 re = new RegExp(/^\/.+\/$/.test(needle) ?
148 needle.slice(1, -1) : 154 needle.slice(1, -1) :
149 regexEscape(needle)); 155 regexEscape(needle));
150 } 156 }
151 157
152 let {owner, property} = findProperty(target) || {}; 158 let {owner, property} = findProperty(target) || {};
159
160 if (!owner)
161 return;
153 162
154 let descriptor = Object.getOwnPropertyDescriptor(owner, property); 163 let descriptor = Object.getOwnPropertyDescriptor(owner, property);
155 if (descriptor && descriptor.get) 164 if (descriptor && descriptor.get)
kzar 2018/07/16 16:33:15 What's the idea with this check?
156 return; 165 return;
157 166
158 let magic = getMagic(); 167 let magic = getMagic();
159 168
160 injectPropertyAccessValidator(owner, property, () => 169 injectPropertyAccessValidator(owner, property, () =>
161 { 170 {
162 let element = document.currentScript; 171 let element = document.currentScript;
163 if (element instanceof HTMLScriptElement && 172 if (element instanceof HTMLScriptElement &&
164 element.src == "" && (!re || re.test(element.textContent))) 173 element.src == "" && (!re || re.test(element.textContent)))
165 { 174 {
166 throw new ReferenceError(magic); 175 throw new ReferenceError(magic);
kzar 2018/07/16 16:33:15 How come you've used `ReferenceError` out of inter
167 } 176 }
168 }); 177 });
169 178
170 suppressMagicError(magic); 179 suppressMagicError(magic);
171 } 180 }
172 181
173 exports["abort-current-inline-script"] = makeInjector(abortCurrentInlineScript); 182 exports["abort-current-inline-script"] = makeInjector(abortCurrentInlineScript);
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