blob: 5fa4f97bf3b5cc21732375ab19463e6ebe3cdb38 [file] [log] [blame]
avm9996304def3e2016-11-27 22:53:05 +01001// 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
5function $(id) {
6 return document.getElementById(id);
7}
8
9chrome.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
26function 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
49function i18nReplace(id, name) {
50 return $(id).innerHTML = chrome.i18n.getMessage(name);
51}
52
53function resizeDivWidth(id, width) {
54 $(id).style.width = width + "px";
55}
56
57function 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);
avm99963873bf6f2021-02-07 00:01:31 +010068
69 $('captureWindowShortcut').style.display = 'none';
70 $('captureAreaShortcut').style.display = 'none';
71 $('captureWebpageShortcut').style.display = 'none';
72 document.body.style.minWidth = "140px";
73
avm9996304def3e2016-11-27 22:53:05 +010074 var isScriptLoad = false;
avm99963476a2a62021-02-07 00:40:02 +010075 chrome.tabs.query({
76 active: true,
Adrià Vilanova Martínez94f91f82024-01-31 17:27:45 +010077 lastFocusedWindow: true,
avm99963476a2a62021-02-07 00:40:02 +010078 }, function(tabs) {
79 if (tabs.length < 1) {
80 console.error('Expected 1 tab');
81 return;
82 }
83 var tab = tabs[0];
avm9996304def3e2016-11-27 22:53:05 +010084 if (tab.url.indexOf('chrome') == 0 || tab.url.indexOf('about') == 0) {
85 i18nReplace('tip', 'special');
86 return;
avm9996304def3e2016-11-27 22:53:05 +010087 }
avm99963476a2a62021-02-07 00:40:02 +010088 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();
avm9996304def3e2016-11-27 22:53:05 +010093
avm99963476a2a62021-02-07 00:40:02 +010094 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ínez94f91f82024-01-31 17:27:45 +0100125 lastFocusedWindow: true,
avm99963476a2a62021-02-07 00:40:02 +0100126 }, 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 });
avm9996304def3e2016-11-27 22:53:05 +0100169 });
170}
171
172function showOption() {
173 $('option').style.display = 'block';
174 $('separatorItem').style.display = 'block';
175}
176
177document.addEventListener('DOMContentLoaded', init);