OLD | NEW |
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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 /* eslint-env node */ | 18 /* eslint-env node */ |
19 /* eslint no-console: "off" */ | 19 /* eslint no-console: "off" */ |
20 | 20 |
21 "use strict"; | 21 "use strict"; |
22 | 22 |
23 const childProcess = require("child_process"); | 23 const childProcess = require("child_process"); |
24 const fs = require("fs"); | 24 const fs = require("fs"); |
25 const https = require("https"); | 25 const https = require("https"); |
26 const os = require("os"); | 26 const os = require("os"); |
27 const path = require("path"); | 27 const path = require("path"); |
28 | 28 |
| 29 const extractZip = require("extract-zip"); |
29 const remoteInterface = require("chrome-remote-interface"); | 30 const remoteInterface = require("chrome-remote-interface"); |
30 const extractZip = require("extract-zip"); | |
31 | 31 |
32 const CHROMIUM_REVISION = 467222; | 32 const CHROMIUM_REVISION = 467222; |
33 | 33 |
34 function rmdir(dirPath) | 34 function rmdir(dirPath) |
35 { | 35 { |
36 for (let file of fs.readdirSync(dirPath)) | 36 for (let file of fs.readdirSync(dirPath)) |
37 { | 37 { |
38 let filePath = path.join(dirPath, file); | 38 let filePath = path.join(dirPath, file); |
39 try | 39 try |
40 { | 40 { |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 return new Promise((resolve, reject) => | 241 return new Promise((resolve, reject) => |
242 { | 242 { |
243 setTimeout(() => | 243 setTimeout(() => |
244 { | 244 { |
245 connectRemoteInterface(attempt + 1).then(resolve).catch(reject); | 245 connectRemoteInterface(attempt + 1).then(resolve).catch(reject); |
246 }, 200); | 246 }, 200); |
247 }); | 247 }); |
248 }); | 248 }); |
249 } | 249 } |
250 | 250 |
251 function runBootstrap(initialPage, bootstrapPath, bootstrapArgs) | 251 function runScript(script, scriptName, scriptArgs) |
252 { | 252 { |
253 return connectRemoteInterface().then(async client => | 253 return connectRemoteInterface().then(async client => |
254 { | 254 { |
255 try | 255 try |
256 { | 256 { |
257 let {Runtime, Log, Console, Page} = client; | 257 let {Runtime, Log, Console} = client; |
258 | 258 |
259 await Log.enable(); | 259 await Log.enable(); |
260 Log.entryAdded(({entry}) => | 260 Log.entryAdded(({entry}) => |
261 { | 261 { |
262 reportMessage(entry.text, entry.level); | 262 reportMessage(entry.text, entry.level); |
263 }); | 263 }); |
264 | 264 |
265 await Console.enable(); | 265 await Console.enable(); |
266 Console.messageAdded(({message}) => | 266 Console.messageAdded(({message}) => |
267 { | 267 { |
268 reportMessage(message.text, message.level); | 268 reportMessage(message.text, message.level); |
269 }); | 269 }); |
270 | 270 |
271 await Page.navigate({url: initialPage}); | |
272 | |
273 await Runtime.enable(); | 271 await Runtime.enable(); |
274 let compileResult = await Runtime.compileScript({ | 272 let compileResult = await Runtime.compileScript({ |
275 expression: fs.readFileSync(bootstrapPath, "utf-8"), | 273 expression: script, |
276 sourceURL: bootstrapPath, | 274 sourceURL: scriptName, |
277 persistScript: true | 275 persistScript: true |
278 }); | 276 }); |
279 if (compileResult.exceptionDetails) | 277 if (compileResult.exceptionDetails) |
280 throwException(compileResult.exceptionDetails, bootstrapPath); | 278 throwException(compileResult.exceptionDetails, scriptName); |
281 | 279 |
282 let runResult = await Runtime.runScript({ | 280 let runResult = await Runtime.runScript({ |
283 scriptId: compileResult.scriptId | 281 scriptId: compileResult.scriptId |
284 }); | 282 }); |
285 if (runResult.exceptionDetails) | 283 if (runResult.exceptionDetails) |
286 throwException(runResult.exceptionDetails, bootstrapPath); | 284 throwException(runResult.exceptionDetails, scriptName); |
287 | 285 |
288 let callResult = await Runtime.callFunctionOn({ | 286 let callResult = await Runtime.callFunctionOn({ |
289 objectId: runResult.result.objectId, | 287 objectId: runResult.result.objectId, |
290 functionDeclaration: "function(...args) {return this(...args);}", | 288 functionDeclaration: "function(...args) { return this(...args); }", |
291 arguments: bootstrapArgs.map(url => ({value: url})) | 289 arguments: scriptArgs.map(arg => ({value: arg})) |
292 }); | 290 }); |
293 if (callResult.exceptionDetails) | 291 if (callResult.exceptionDetails) |
294 throwException(callResult.exceptionDetails, bootstrapPath); | 292 throwException(callResult.exceptionDetails, scriptName); |
295 | 293 |
296 let promiseResult = await Runtime.awaitPromise({ | 294 let promiseResult = await Runtime.awaitPromise({ |
297 promiseObjectId: callResult.result.objectId | 295 promiseObjectId: callResult.result.objectId |
298 }); | 296 }); |
299 if (promiseResult.exceptionDetails) | 297 if (promiseResult.exceptionDetails) |
300 throwException(promiseResult.exceptionDetails, bootstrapPath); | 298 throwException(promiseResult.exceptionDetails, scriptName); |
301 } | 299 } |
302 finally | 300 finally |
303 { | 301 { |
304 client.close(); | 302 client.close(); |
305 } | 303 } |
306 }); | 304 }); |
307 } | 305 } |
308 | 306 |
309 module.exports = function(initialPage, bootstrapPath, bootstrapArgs) | 307 module.exports = function(script, scriptName, ...scriptArgs) |
310 { | 308 { |
311 return ensureChromium().then(chromiumPath => | 309 return ensureChromium().then(chromiumPath => |
312 { | 310 { |
313 let child = startChromium(chromiumPath); | 311 let child = startChromium(chromiumPath); |
314 return Promise.race([ | 312 return Promise.race([ |
315 child.done, | 313 child.done, |
316 runBootstrap(initialPage, bootstrapPath, bootstrapArgs) | 314 runScript(script, scriptName, scriptArgs) |
317 ]).then(result => | 315 ]).then(result => |
318 { | 316 { |
319 child.kill(); | 317 child.kill(); |
320 return result; | 318 return result; |
321 }).catch(error => | 319 }).catch(error => |
322 { | 320 { |
323 child.kill(); | 321 child.kill(); |
324 throw error; | 322 throw error; |
325 }); | 323 }); |
326 }); | 324 }); |
327 }; | 325 }; |
OLD | NEW |