avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
|
| 2 | // source code is governed by a BSD-style license that can be found in the
|
| 3 | // LICENSE file. |
| 4 | |
| 5 | function $(id) { |
| 6 | return document.getElementById(id); |
| 7 | } |
| 8 | |
| 9 | chrome.extension.onMessage.addListener(function(request, sender, response) { |
| 10 | if (request.msg == 'page_capturable') { |
| 11 | $('tip').style.display = 'none'; |
| 12 | $('captureSpecialPageItem').style.display = 'none'; |
| 13 | $('captureWindowItem').style.display = 'block'; |
| 14 | $('captureAreaItem').style.display = 'block'; |
| 15 | $('captureWebpageItem').style.display = 'block'; |
| 16 | } else if (request.msg == 'page_uncapturable') { |
| 17 | i18nReplace('tip', 'special'); |
| 18 | $('tip').style.display = 'block'; |
| 19 | $('captureSpecialPageItem').style.display = 'none'; |
| 20 | $('captureWindowItem').style.display = 'none'; |
| 21 | $('captureAreaItem').style.display = 'none'; |
| 22 | $('captureWebpageItem').style.display = 'none'; |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | function toDo(what) { |
| 27 | var bg = chrome.extension.getBackgroundPage(); |
| 28 | switch (what) { |
| 29 | case 'capture_window': |
| 30 | bg.screenshot.captureWindow(); |
| 31 | window.close(); |
| 32 | break; |
| 33 | case 'capture_area': |
| 34 | bg.screenshot.showSelectionArea(); |
| 35 | window.close(); |
| 36 | break; |
| 37 | case 'capture_webpage': |
| 38 | bg.screenshot.captureWebpage(); |
| 39 | $('loadDiv').style.display = 'block'; |
| 40 | $('item').style.display = 'none'; |
| 41 | break; |
| 42 | case 'capture_special_page': |
| 43 | bg.screenshot.captureSpecialPage(); |
| 44 | window.close(); |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function i18nReplace(id, name) { |
| 50 | return $(id).innerHTML = chrome.i18n.getMessage(name); |
| 51 | } |
| 52 | |
| 53 | function resizeDivWidth(id, width) { |
| 54 | $(id).style.width = width + "px"; |
| 55 | } |
| 56 | |
| 57 | function init() { |
| 58 | i18nReplace('captureSpecialPageText', 'capture_window'); |
| 59 | i18nReplace('capturing', 'capturing'); |
| 60 | i18nReplace('captureWindowText', 'capture_window'); |
| 61 | i18nReplace('captureAreaText', 'capture_area'); |
| 62 | i18nReplace('captureWebpageText', 'capture_webpage'); |
| 63 | i18nReplace('optionItem', 'option'); |
| 64 | |
| 65 | $('option').addEventListener('click', function () { |
| 66 | chrome.tabs.create({ url: 'options.html'}); |
| 67 | }, false); |
avm99963 | 873bf6f | 2021-02-07 00:01:31 +0100 | [diff] [blame] | 68 | |
| 69 | $('captureWindowShortcut').style.display = 'none'; |
| 70 | $('captureAreaShortcut').style.display = 'none'; |
| 71 | $('captureWebpageShortcut').style.display = 'none'; |
| 72 | document.body.style.minWidth = "140px"; |
| 73 | |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 74 | var isScriptLoad = false; |
avm99963 | 476a2a6 | 2021-02-07 00:40:02 +0100 | [diff] [blame] | 75 | chrome.tabs.query({ |
| 76 | active: true, |
Adrià Vilanova Martínez | 94f91f8 | 2024-01-31 17:27:45 +0100 | [diff] [blame] | 77 | lastFocusedWindow: true, |
avm99963 | 476a2a6 | 2021-02-07 00:40:02 +0100 | [diff] [blame] | 78 | }, function(tabs) { |
| 79 | if (tabs.length < 1) { |
| 80 | console.error('Expected 1 tab'); |
| 81 | return; |
| 82 | } |
| 83 | var tab = tabs[0]; |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 84 | if (tab.url.indexOf('chrome') == 0 || tab.url.indexOf('about') == 0) { |
| 85 | i18nReplace('tip', 'special'); |
| 86 | return; |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 87 | } |
avm99963 | 476a2a6 | 2021-02-07 00:40:02 +0100 | [diff] [blame] | 88 | chrome.tabs.executeScript({ file: 'js/page.js' }, _ => { |
| 89 | chrome.tabs.executeScript(null, {file: 'js/isLoad.js'}, _ => { |
| 90 | $('tip').style.display = 'none'; |
| 91 | $('captureSpecialPageItem').style.display = 'block'; |
| 92 | showOption(); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 93 | |
avm99963 | 476a2a6 | 2021-02-07 00:40:02 +0100 | [diff] [blame] | 94 | chrome.tabs.sendMessage(tab.id, {msg: 'is_page_capturable'}, |
| 95 | response => { |
| 96 | isScriptLoad = true; |
| 97 | if (response.msg == 'capturable') { |
| 98 | $('tip').style.display = 'none'; |
| 99 | $('captureSpecialPageItem').style.display = 'none'; |
| 100 | $('captureWindowItem').style.display = 'block'; |
| 101 | $('captureAreaItem').style.display = 'block'; |
| 102 | $('captureWebpageItem').style.display = 'block'; |
| 103 | var textWidth = $('captureWindowText')['scrollWidth']; |
| 104 | resizeDivWidth('captureWindowText', textWidth); |
| 105 | resizeDivWidth('captureAreaText', textWidth); |
| 106 | resizeDivWidth('captureWebpageText', textWidth); |
| 107 | var bg = chrome.extension.getBackgroundPage(); |
| 108 | if (bg.screenshot.isThisPlatform('mac')) { |
| 109 | $('captureAreaShortcut').innerText = '\u2325\u2318R'; |
| 110 | $('captureWindowShortcut').innerText = '\u2325\u2318V'; |
| 111 | $('captureWebpageShortcut').innerText = '\u2325\u2318H'; |
| 112 | } |
| 113 | } else if (response.msg == 'uncapturable') { |
| 114 | i18nReplace('tip', 'special'); |
| 115 | $('tip').style.display = 'block'; |
| 116 | } else { |
| 117 | i18nReplace('tip', 'loading'); |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | var insertScript = function() { |
| 122 | if (isScriptLoad == false) { |
| 123 | chrome.tabs.query({ |
| 124 | active: true, |
Adrià Vilanova Martínez | 94f91f8 | 2024-01-31 17:27:45 +0100 | [diff] [blame] | 125 | lastFocusedWindow: true, |
avm99963 | 476a2a6 | 2021-02-07 00:40:02 +0100 | [diff] [blame] | 126 | }, function(tabs) { |
| 127 | if (tabs.length < 1) { |
| 128 | console.error('Expected 1 tab.'); |
| 129 | return; |
| 130 | } |
| 131 | var tab = tabs[0]; |
| 132 | if (tab.url.indexOf('chrome') == 0 || |
| 133 | tab.url.indexOf('about') == 0) { |
| 134 | i18nReplace('tip', 'special'); |
| 135 | } else { |
| 136 | $('tip').style.display = 'none'; |
| 137 | $('captureSpecialPageItem').style.display = 'block'; |
| 138 | showOption(); |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | var captureItems = document.querySelectorAll('li.menuI'); |
| 143 | var showSeparator = false; |
| 144 | for (var i = 0; i < captureItems.length; i++) { |
| 145 | if (window.getComputedStyle(captureItems[i]).display != 'none') { |
| 146 | showSeparator = true; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | $('separatorItem').style.display = showSeparator ? 'block' : 'none'; |
| 151 | } |
| 152 | setTimeout(insertScript, 500); |
| 153 | |
| 154 | // Update hot key. |
| 155 | $('captureSpecialPageItem').addEventListener('click', function(e) { |
| 156 | toDo('capture_special_page'); |
| 157 | }); |
| 158 | $('captureAreaItem').addEventListener('click', function(e) { |
| 159 | toDo('capture_area'); |
| 160 | }); |
| 161 | $('captureWindowItem').addEventListener('click', function(e) { |
| 162 | toDo('capture_window'); |
| 163 | }); |
| 164 | $('captureWebpageItem').addEventListener('click', function(e) { |
| 165 | toDo('capture_webpage'); |
| 166 | }); |
| 167 | }); |
| 168 | }); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 169 | }); |
| 170 | } |
| 171 | |
| 172 | function showOption() { |
| 173 | $('option').style.display = 'block'; |
| 174 | $('separatorItem').style.display = 'block'; |
| 175 | } |
| 176 | |
| 177 | document.addEventListener('DOMContentLoaded', init); |