Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import codecs | |
| 7 import os | |
| 8 import sys | |
| 9 | |
| 10 TELEMETRY_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') | |
| 11 sys.path.append(TELEMETRY_DIR) | |
| 12 | |
| 13 from telemetry.util import wpr_modes | |
| 14 from telemetry.core import util | |
| 15 from telemetry.internal.browser import browser_finder | |
| 16 from telemetry.internal.browser import browser_options | |
| 17 from telemetry.internal.util import binary_manager | |
| 18 | |
| 19 | |
| 20 def SnapPage(finder_options, url, snapshot_path): | |
| 21 """ Save the HTML snapshot of the page whose address |url| to |snapshot_path|. | |
|
wkorman
2017/09/02 13:50:06
"is" after "address"
nednguyen
2017/09/05 17:49:08
Done.
| |
| 22 """ | |
| 23 snapshot_path = os.path.abspath(snapshot_path) | |
| 24 binary_manager.InitDependencyManager([]) | |
| 25 possible_browser = browser_finder.FindBrowser(finder_options) | |
| 26 browser = possible_browser.Create(finder_options) | |
| 27 try: | |
| 28 tab = browser.tabs[0] | |
| 29 tab.Navigate(url) | |
| 30 tab.WaitForDocumentReadyStateToBeComplete() | |
| 31 with open(os.path.join(util.GetTelemetryThirdPartyDir(), 'snap-it', | |
| 32 'HTMLSerializer.js')) as f: | |
| 33 snapit_script = f.read() | |
| 34 tab.ExecuteJavaScript(snapit_script) | |
| 35 tab.ExecuteJavaScript( | |
| 36 ''' | |
| 37 var serializedDomArray; | |
| 38 var htmlSerializer = new HTMLSerializer(); | |
| 39 htmlSerializer.processDocument(document); | |
| 40 htmlSerializer.fillHolesAsync(document, function(s) { | |
| 41 serializedDomArray = s.html; | |
| 42 }); | |
| 43 ''') | |
| 44 print 'Snapshotting content of %s. This could take a while...' % url | |
| 45 tab.WaitForJavaScriptCondition('serializedDomArray !== undefined') | |
| 46 serialized_dom = ''.join(tab.EvaluateJavaScript('serializedDomArray')) | |
| 47 with codecs.open(snapshot_path, 'w', 'utf-8') as f: | |
| 48 f.write(serialized_dom) | |
| 49 print 'Successfuly saved snapshot to file://%s' % snapshot_path | |
|
wkorman
2017/09/02 13:50:06
'lly' vs 'ly'
nednguyen
2017/09/05 17:49:08
Done.
| |
| 50 finally: | |
| 51 browser.Close() | |
| 52 | |
| 53 | |
| 54 def main(args): | |
| 55 options = browser_options.BrowserFinderOptions() | |
| 56 options.browser_options.wpr_mode = wpr_modes.WPR_OFF | |
| 57 parser = options.CreateParser( | |
| 58 usage='Create a static HTML snapshot of the page') | |
| 59 parser.add_option('--url', help='URL of the web page to record') | |
| 60 parser.add_option('--snapshot-path', help='Where to save the snapshot', | |
| 61 default='snapshot.html') | |
| 62 parser.parse_args(args) | |
| 63 SnapPage(options, options.url, options.snapshot_path) | |
| 64 | |
| 65 if __name__ == '__main__': | |
| 66 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |