blob: 9d6149829f279861faf2798f614979276c7750f6 [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
5var bg = chrome.extension.getBackgroundPage();
6
7function $(id) {
8 return document.getElementById(id);
9}
10
11function isHighVersion() {
12 var version = navigator.userAgent.match(/Chrome\/(\d+)/)[1];
13 return version > 9;
14}
15
16function init() {
17 i18nReplace('optionTitle', 'options');
18 i18nReplace('saveAndClose', 'save_and_close');
19 i18nReplace('screenshootQualitySetting', 'quality_setting');
20 i18nReplace('lossyScreenShot', 'lossy');
21 i18nReplace('losslessScreenShot', 'lossless');
22 i18nReplace('shorcutSetting', 'shortcut_setting');
23 i18nReplace('settingShortcutText', 'shortcutsetting_text');
24 if (isHighVersion()) {
25 $('lossyScreenShot').innerText += ' (JPEG)';
26 $('losslessScreenShot').innerText += ' (PNG)';
27 }
28 $('saveAndClose').addEventListener('click', saveAndClose);
29 initScreenCaptureQuality();
30 HotKeySetting.setup();
31}
32
33function save() {
34 localStorage.screenshootQuality =
35 $('lossy').checked ? 'jpeg' : '' ||
36 $('lossless').checked ? 'png' : '';
37
38 return HotKeySetting.save();
39}
40
41function saveAndClose() {
42 if (save())
43 chrome.tabs.getSelected(null, function(tab) {
44 chrome.tabs.remove(tab.id);
45 });
46}
47
48function initScreenCaptureQuality() {
49 $('lossy').checked = localStorage.screenshootQuality == 'jpeg';
50 $('lossless').checked = localStorage.screenshootQuality == 'png';
51}
52
53function i18nReplace(id, name) {
54 return $(id).innerText = chrome.i18n.getMessage(name);
55}
56
57const CURRENT_LOCALE = chrome.i18n.getMessage('@@ui_locale');
58if (CURRENT_LOCALE != 'zh_CN') {
59 UI.addStyleSheet('./i18n_styles/en_options.css');
60}
61
62var HotKeySetting = (function() {
63 const CHAR_CODE_OF_AT = 64;
64 const CHAR_CODE_OF_A = 65;
65 const CHAR_CODE_OF_Z = 90;
66 var hotKeySelection = null;
67
68 var hotkey = {
69 setup: function() {
70 hotKeySelection = document.querySelectorAll('#hot-key-setting select');
71 // i18n.
72 $('area-capture-text').innerText =
73 chrome.i18n.getMessage('capture_area');
74 $('viewport-capture-text').innerText =
75 chrome.i18n.getMessage('capture_window');
76 $('full-page-capture-text').innerText =
77 chrome.i18n.getMessage('capture_webpage');
78 //$('screen-capture-text').innerText = chrome.i18n.getMessage('capture_screen');
79
80 for (var i = 0; i < hotKeySelection.length; i++) {
81 hotKeySelection[i].add(new Option('--', '@'));
82 for (var j = CHAR_CODE_OF_A; j <= CHAR_CODE_OF_Z; j++) {
83 var value = String.fromCharCode(j);
84 var option = new Option(value, value);
85 hotKeySelection[i].add(option);
86 }
87 }
88
89 $('area-capture-hot-key').selectedIndex =
90 HotKey.getCharCode('area') - CHAR_CODE_OF_AT;
91 $('viewport-capture-hot-key').selectedIndex =
92 HotKey.getCharCode('viewport') - CHAR_CODE_OF_AT;
93 $('full-page-capture-hot-key').selectedIndex =
94 HotKey.getCharCode('fullpage') - CHAR_CODE_OF_AT;
95 $('screen-capture-hot-key').selectedIndex =
96 HotKey.getCharCode('screen') - CHAR_CODE_OF_AT;
97
98 $('settingShortcut').addEventListener('click', function() {
99 hotkey.setState(this.checked);
100 }, false);
101
102 hotkey.setState(HotKey.isEnabled());
103 },
104
105 validate: function() {
106 var hotKeyLength =
107 Array.prototype.filter.call(hotKeySelection,
108 function (element) {
109 return element.value != '@'
110 }
111 ).length;
112 if (hotKeyLength != 0) {
113 var validateMap = {};
114 validateMap[hotKeySelection[0].value] = true;
115 validateMap[hotKeySelection[1].value] = true;
116 validateMap[hotKeySelection[2].value] = true;
117 if (hotKeyLength > 3 && hotKeySelection[3].value != '@') {
118 hotKeyLength -= 1;
119 }
120
121 if (Object.keys(validateMap).length < hotKeyLength) {
122 ErrorInfo.show('hot_key_conflict');
123 return false;
124 }
125 }
126 ErrorInfo.hide();
127 return true;
128 },
129
130 save: function() {
131 var result = true;
132 if ($('settingShortcut').checked) {
133 if (this.validate()) {
134 HotKey.enable();
135 HotKey.set('area', $('area-capture-hot-key').value);
136 HotKey.set('viewport', $('viewport-capture-hot-key').value);
137 HotKey.set('fullpage', $('full-page-capture-hot-key').value);
138 } else {
139 result = false;
140 }
141 } else {
142 HotKey.disable(bg);
143 }
144 return result;
145 },
146
147 setState: function(enabled) {
148 $('settingShortcut').checked = enabled;
149 UI.setStyle($('hot-key-setting'), 'color', enabled ? '' : '#6d6d6d');
150 for (var i = 0; i < hotKeySelection.length; i++) {
151 hotKeySelection[i].disabled = !enabled;
152 }
153 ErrorInfo.hide();
154 },
155
156 focusScreenCapture: function() {
157 $('screen-capture-hot-key').focus();
158 }
159 };
160 return hotkey;
161})();
162
163var ErrorInfo = (function() {
164 return {
165 show: function(msgKey) {
166 var infoWrapper = $('error-info');
167 var msg = chrome.i18n.getMessage(msgKey);
168 infoWrapper.innerText = msg;
169 UI.show(infoWrapper);
170 },
171
172 hide: function() {
173 var infoWrapper = $('error-info');
174 if (infoWrapper) {
175 UI.hide(infoWrapper);
176 }
177 }
178 };
179})();
180
181document.addEventListener('DOMContentLoaded', init);