Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: chrome/common/extensions/docs/examples/api/tabs/popup.js

Issue 398823002: Sample Extension for Tabs Zoom API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /**
2 * @fileoverview This code supports the popup behaviour of the extension, and
3 * demonstrates how to:
4 *
5 * 1) Set the zoom for a tab using tabs.setZoom()
6 * 2) Read the current zoom of a tab using tabs.getZoom()
7 * 3) Set the zoom mode of a tab using tabs.setZoomSettings()
8 * 4) Read the current zoom mode of a tab using
9 * tabs.getZoomSettings()
10 *
11 * It also demonstrates using a zoom change listener to update the
12 * contents of a control.
13 */
14
15 tabId = -1;
16 zoomStep = 1.1;
17
18 function displayZoomLevel(level) {
19 var percentZoom = parseFloat(level) * 100;
20 var zoom_percent_str = percentZoom.toFixed(1) + '%';
21
22 document.getElementById('displayButton').textContent = zoom_percent_str;
23 }
24
25 document.addEventListener('DOMContentLoaded', function() {
26 chrome.tabs.query({active: true}, function (tabs) {
27 if (tabs.length > 1)
28 console.log('TabsQuery: ' + tabs)
29 tabId= tabs[0].id;
raymes 2014/07/17 00:39:10 nit: tabId = This seems like it is just going to u
wjmaclean 2014/07/17 18:46:26 I thought the query would only ever return the cur
raymes 2014/07/18 00:10:05 Ah, my bad it seems like your query would correctl
30
31 chrome.tabs.getZoomSettings(tabId, function(zoomSettings) {
32 var modeRadios = document.getElementsByName('modeRadio');
33 for (var i = 0; i < modeRadios.length; i++)
raymes 2014/07/17 00:39:10 need {} around this multi-line for loop (here and
wjmaclean 2014/07/17 18:46:27 Done.
34 if (modeRadios[i].value == zoomSettings.mode)
35 modeRadios[i].checked = true;
36
37 var scopeRadios = document.getElementsByName('scopeRadio');
38 for (var i = 0; i < scopeRadios.length; i++)
39 if (scopeRadios[i].value == zoomSettings.scope)
40 scopeRadios[i].checked = true;
41 });
42
43 chrome.tabs.getZoom(tabId, displayZoomLevel);
44 });
45
46 document.getElementById('increaseButton').onclick = doZoomIn;
47 document.getElementById('decreaseButton').onclick = doZoomOut;
48 document.getElementById('defaultButton').onclick = doZoomDefault;
49 document.getElementById('setModeButton').onclick = doSetMode;
50 document.getElementById('closeButton').onclick = doClose;
51 });
52
53 function zoomChangeListener(zoomChangeInfo) {
54 displayZoomLevel(zoomChangeInfo.newZoomFactor);
55 }
56
57 chrome.tabs.onZoomChange.addListener(zoomChangeListener);
58
59 function changeZoomByFactorDelta(factorDelta) {
60 if (tabId == -1)
61 return;
62
63 chrome.tabs.getZoom(tabId, function(zoomFactor) {
64 currentZoomFactor = zoomFactor;
raymes 2014/07/17 00:39:10 I think you can get rid of the "currentZoomFactor"
wjmaclean 2014/07/17 18:46:27 Done.
65 var newZoomFactor = factorDelta * currentZoomFactor;
66 chrome.tabs.setZoom(tabId, newZoomFactor, function() {
67 if (chrome.runtime.lastError)
68 console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message);
69 });
70 });
71 }
72
73 function doZoomIn() {
74 changeZoomByFactorDelta(zoomStep);
75 }
76
77 function doZoomOut() {
78 changeZoomByFactorDelta(1.0/zoomStep);
79 }
80
81 function doZoomDefault() {
82 if (tabId == -1)
83 return;
84
85 chrome.tabs.setZoom(tabId, 1.0);
86 }
87
88 function doSetMode() {
89 if (tabId == -1)
90 return;
91
92 var modeVal;
93 var modeRadios = document.getElementsByName('modeRadio');
94 for (var i = 0; i < modeRadios.length; i++) {
95 if (modeRadios[i].checked)
96 modeVal = modeRadios[i].value;
97 }
98
99 var scopeVal;
100 var scopeRadios = document.getElementsByName('scopeRadio');
101 for (var i = 0; i < scopeRadios.length; i++) {
102 if (scopeRadios[i].checked)
103 scopeVal = scopeRadios[i].value;
104 }
105
106 if ((modeVal === undefined) || (scopeVal === undefined)) {
raymes 2014/07/17 00:39:10 if (!modeVal || !scopeVal)
wjmaclean 2014/07/17 18:46:27 Done.
107 alert('You must specify values for both mode & scope first.');
108 self.close();
raymes 2014/07/17 00:39:10 My guess is that this case should never really hap
wjmaclean 2014/07/17 18:46:27 Done.
109 return;
110 }
111
112 chrome.tabs.setZoomSettings(tabId, { mode: modeVal, scope: scopeVal },
113 function() {
114 if (chrome.runtime.lastError)
raymes 2014/07/17 00:39:10 nit: {} for multi-line if
wjmaclean 2014/07/17 18:46:27 Done.
115 console.log('[ZoomDemoExtension] doSetMode() error: ' +
116 chrome.runtime.lastError.message);
117 });
118 }
119
120 function doClose() {
121 self.close();
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698