-
Notifications
You must be signed in to change notification settings - Fork 459
/
popup.js
141 lines (117 loc) · 4.32 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview This code supports the popup behaviour of the extension, and
* demonstrates how to:
*
* 1) Set the zoom for a tab using tabs.setZoom()
* 2) Read the current zoom of a tab using tabs.getZoom()
* 3) Set the zoom mode of a tab using tabs.setZoomSettings()
* 4) Read the current zoom mode of a tab using
* tabs.getZoomSettings()
*
* It also demonstrates using a zoom change listener to update the
* contents of a control.
*/
zoomStep = 1.1;
tabId = -1;
function displayZoomLevel(level) {
var percentZoom = parseFloat(level) * 100;
var zoom_percent_str = percentZoom.toFixed(1) + '%';
document.getElementById('displayDiv').textContent = zoom_percent_str;
}
document.addEventListener('DOMContentLoaded', function() {
// Find the tabId of the current (active) tab. We could just omit the tabId
// parameter in the function calls below, and they would act on the current
// tab by default, but for the purposes of this demo we will always use the
// API with an explicit tabId to demonstrate its use.
chrome.tabs.query({active: true}, function (tabs) {
if (tabs.length > 1)
console.log(
'[ZoomDemoExtension] Query unexpectedly returned more than 1 tab.');
tabId = tabs[0].id;
chrome.tabs.getZoomSettings(tabId, function(zoomSettings) {
var modeRadios = document.getElementsByName('modeRadio');
for (var i = 0; i < modeRadios.length; i++) {
if (modeRadios[i].value == zoomSettings.mode)
modeRadios[i].checked = true;
}
var scopeRadios = document.getElementsByName('scopeRadio');
for (var i = 0; i < scopeRadios.length; i++) {
if (scopeRadios[i].value == zoomSettings.scope)
scopeRadios[i].checked = true;
}
var percentDefaultZoom =
parseFloat(zoomSettings.defaultZoomFactor) * 100;
document.getElementById('defaultLabel').textContent =
'Default: ' + percentDefaultZoom.toFixed(1) + '%';
});
chrome.tabs.getZoom(tabId, displayZoomLevel);
});
document.getElementById('increaseButton').onclick = doZoomIn;
document.getElementById('decreaseButton').onclick = doZoomOut;
document.getElementById('defaultButton').onclick = doZoomDefault;
document.getElementById('setModeButton').onclick = doSetMode;
document.getElementById('closeButton').onclick = doClose;
});
function zoomChangeListener(zoomChangeInfo) {
displayZoomLevel(zoomChangeInfo.newZoomFactor);
}
chrome.tabs.onZoomChange.addListener(zoomChangeListener);
function changeZoomByFactorDelta(factorDelta) {
if (tabId == -1)
return;
chrome.tabs.getZoom(tabId, function(zoomFactor) {
var newZoomFactor = factorDelta * zoomFactor;
chrome.tabs.setZoom(tabId, newZoomFactor, function() {
if (chrome.runtime.lastError)
console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message);
});
});
}
function doZoomIn() {
changeZoomByFactorDelta(zoomStep);
}
function doZoomOut() {
changeZoomByFactorDelta(1.0/zoomStep);
}
function doZoomDefault() {
if (tabId == -1)
return;
chrome.tabs.setZoom(tabId, 0, function() {
if (chrome.runtime.lastError)
console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message);
});
}
function doSetMode() {
if (tabId == -1)
return;
var modeVal;
var modeRadios = document.getElementsByName('modeRadio');
for (var i = 0; i < modeRadios.length; i++) {
if (modeRadios[i].checked)
modeVal = modeRadios[i].value;
}
var scopeVal;
var scopeRadios = document.getElementsByName('scopeRadio');
for (var i = 0; i < scopeRadios.length; i++) {
if (scopeRadios[i].checked)
scopeVal = scopeRadios[i].value;
}
if (!modeVal || !scopeVal) {
console.log(
'[ZoomDemoExtension] Must specify values for both mode & scope.');
return;
}
chrome.tabs.setZoomSettings(tabId, { mode: modeVal, scope: scopeVal },
function() {
if (chrome.runtime.lastError) {
console.log('[ZoomDemoExtension] doSetMode() error: ' +
chrome.runtime.lastError.message);
}
});
}
function doClose() {
self.close();
}