forked from Cloud-Player/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.js
65 lines (53 loc) · 2.35 KB
/
phantom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
// Optional: set logging level of launcher to see its output.
// Install it using: yarn add lighthouse-logger
// const log = require('lighthouse-logger');
// log.setLevel('info');
/**
* Launches a debugging instance of Chrome.
* @param {boolean=} headless True (default) launches Chrome in headless mode.
* False launches a full version of Chrome.
* @return {Promise<ChromeLauncher>}
*/
function launchChrome(headless) {
return chromeLauncher.launch({
enableExtensions: true,
// port: 9222, // Uncomment to force a specific port of your choice.
chromeFlags: [
'--mute-audio=false',
'--window-size=0,0',
'--disable-gpu',
headless ? '--headless' : ''
]
});
}
launchChrome(true).then(function(chrome){
_asyncToGenerator(function* () {
var chrome = yield launchChrome();
var protocol = yield CDP({ port: chrome.port });
// Extract the DevTools protocol domains we need and enable them.
// See API docs: https://chromedevtools.github.io/devtools-protocol/
var Page = protocol.Page;
var Runtime = protocol.Runtime;
yield Page.enable();
Page.navigate({ url: 'http://youtube.com/' });
// Wait for window.onload before doing stuff.
Page.loadEventFired(_asyncToGenerator(function* () {
var manifest = yield Page.getAppManifest();
if (manifest.url) {
console.log('Manifest: ' + manifest.url);
console.log(manifest.data);
} else {
console.log('Site has no app manifest');
}
const js = "document.querySelector('title').textContent";
// Evaluate the JS expression in the page.
const result = yield Runtime.evaluate({expression: js});
console.log('Title of page: ' + result.result.value);
//protocol.close();
//chrome.kill(); // Kill Chrome.
}));
})();
});