Просмотр исходного кода

fixed CSP issue when injecting /hooks/hooks-web.js

Gildas 6 лет назад
Родитель
Сommit
c1ac48cb26

+ 1 - 1
cli/back-ends/jsdom.js

@@ -83,7 +83,7 @@ exports.getPageData = async options => {
 		const win = dom.window;
 		const doc = win.document;
 		let scripts = (await Promise.all(SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()))).join("\n");
-		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
+		scripts = "this.getFileContent = () => `" + fs.readFileSync(require.resolve("../../lib/hooks/hooks-web.js")) + "`;" + scripts;
 		dom.window.eval(scripts);
 		if (dom.window.document.readyState == "loading") {
 			await new Promise(resolve => win.document.onload = resolve);

+ 1 - 1
cli/back-ends/puppeteer.js

@@ -86,7 +86,7 @@ exports.getPageData = async options => {
 			await page.setBypassCSP(true);
 		}
 		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
-		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
+		scripts = "this.getFileContent = () => `" + fs.readFileSync(require.resolve("../../lib/hooks/hooks-web.js")) + "`;" + scripts;
 		await page.evaluateOnNewDocument(scripts);
 		if (options.browserDebug) {
 			await page.waitFor(3000);

+ 1 - 1
cli/back-ends/webdriver-chromium.js

@@ -107,7 +107,7 @@ exports.getPageData = async options => {
 			}
 		}
 		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
-		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
+		scripts = "this.getFileContent = () => `" + fs.readFileSync(require.resolve("../../lib/hooks/hooks-web.js")) + "`;" + scripts;
 		if (options.browserDebug) {
 			await driver.sleep(3000);
 		}

+ 1 - 1
cli/back-ends/webdriver-gecko.js

@@ -97,7 +97,7 @@ exports.getPageData = async options => {
 			}
 		}
 		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString().replace(/\n(this)\.([^ ]+) = (this)\.([^ ]+) \|\|/g, "\nwindow.$2 = window.$4 ||")).join("\n");
-		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
+		scripts = "this.getFileContent = () => `" + fs.readFileSync(require.resolve("../../lib/hooks/hooks-web.js")) + "`;" + scripts;
 		if (options.browserDebug) {
 			await driver.findElement(By.css("html")).sendKeys(Key.SHIFT + Key.F5);
 			await driver.sleep(3000);