| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 3 # | 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 7 # | 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # | 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 if os.path.exists(script): | 46 if os.path.exists(script): |
| 47 with open(script) as f: | 47 with open(script) as f: |
| 48 lines.extend(f.read()) | 48 lines.extend(f.read()) |
| 49 elif util.resource_exists(script): | 49 elif util.resource_exists(script): |
| 50 lines.extend(util.resource_string(script)) | 50 lines.extend(util.resource_string(script)) |
| 51 else: | 51 else: |
| 52 raise Exception('Script does not exist: %s', script) | 52 raise Exception('Script does not exist: %s', script) |
| 53 | 53 |
| 54 script_template = jsmin.jsmin(''.join(lines), quote_chars="'\"`") | 54 script_template = jsmin.jsmin(''.join(lines), quote_chars="'\"`") |
| 55 def injector(record_time): | 55 def injector(record_time): |
| 56 js_timestamp = int((record_time - datetime.datetime(1970, 1, 1)) | 56 delta = record_time - datetime.datetime(1970, 1, 1) |
| 57 .total_seconds()) * 1000 | 57 js_timestamp = \ |
| 58 int(delta.total_seconds()) * 1000 + delta.microseconds / 1000 |
| 58 return script_template.replace(TIME_SEED_MARKER, str(js_timestamp)) | 59 return script_template.replace(TIME_SEED_MARKER, str(js_timestamp)) |
| 59 return injector | 60 return injector |
| 60 | 61 |
| 61 | 62 |
| 62 def _IsHtmlContent(content): | 63 def _IsHtmlContent(content): |
| 63 content = content.strip() | 64 content = content.strip() |
| 64 return content.startswith('<') and content.endswith('>') | 65 return content.startswith('<') and content.endswith('>') |
| 65 | 66 |
| 66 | 67 |
| 67 def InjectScript(text_chunks, content_type, script_to_inject): | 68 def InjectScript(text_chunks, content_type, script_to_inject): |
| (...skipping 27 matching lines...) Expand all Loading... |
| 95 script_to_inject, | 96 script_to_inject, |
| 96 chunk[pos:]) | 97 chunk[pos:]) |
| 97 return result, True | 98 return result, True |
| 98 pos -= len(chunk) | 99 pos -= len(chunk) |
| 99 result = text_chunks[:] | 100 result = text_chunks[:] |
| 100 result[0] = '<script>%s</script>%s' % (script_to_inject, | 101 result[0] = '<script>%s</script>%s' % (script_to_inject, |
| 101 text_chunks[0]) | 102 text_chunks[0]) |
| 102 logging.warning('Inject at the very beginning, because no tag of ' | 103 logging.warning('Inject at the very beginning, because no tag of ' |
| 103 '<head>, <html> or <!doctype html> is found.') | 104 '<head>, <html> or <!doctype html> is found.') |
| 104 return result, True | 105 return result, True |
| OLD | NEW |