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

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

Issue 398823002: Sample Extension for Tabs Zoom API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments, add runtime.lastError check. 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 zoomStep = 1.1;
16
17 function displayZoomLevel(level) {
18 var percentZoom = parseFloat(level) * 100;
19 var zoom_percent_str = percentZoom.toFixed(1) + '%';
20
21 document.getElementById('displayDiv').textContent = zoom_percent_str;
22 }
23
24 document.addEventListener('DOMContentLoaded', function() {
25 chrome.tabs.getZoomSettings(function(zoomSettings) {
26 var modeRadios = document.getElementsByName('modeRadio');
27 for (var i = 0; i < modeRadios.length; i++) {
28 if (modeRadios[i].value == zoomSettings.mode)
29 modeRadios[i].checked = true;
30 }
31
32 var scopeRadios = document.getElementsByName('scopeRadio');
33 for (var i = 0; i < scopeRadios.length; i++) {
34 if (scopeRadios[i].value == zoomSettings.scope)
35 scopeRadios[i].checked = true;
36 }
37 });
38
39 chrome.tabs.getZoom(displayZoomLevel);
raymes 2014/07/18 00:10:06 All the code in this function up to here seems ind
wjmaclean 2014/07/18 18:12:49 Done.
40
41 document.getElementById('increaseButton').onclick = doZoomIn;
42 document.getElementById('decreaseButton').onclick = doZoomOut;
43 document.getElementById('defaultButton').onclick = doZoomDefault;
44 document.getElementById('setModeButton').onclick = doSetMode;
45 document.getElementById('closeButton').onclick = doClose;
46 });
47
48 function zoomChangeListener(zoomChangeInfo) {
49 displayZoomLevel(zoomChangeInfo.newZoomFactor);
50 }
51
52 chrome.tabs.onZoomChange.addListener(zoomChangeListener);
53
54 function changeZoomByFactorDelta(factorDelta) {
55 chrome.tabs.getZoom(function(zoomFactor) {
56 var newZoomFactor = factorDelta * zoomFactor;
57 chrome.tabs.setZoom(newZoomFactor, function() {
58 if (chrome.runtime.lastError)
59 console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message);
60 });
61 });
62 }
63
64 function doZoomIn() {
65 changeZoomByFactorDelta(zoomStep);
66 }
67
68 function doZoomOut() {
69 changeZoomByFactorDelta(1.0/zoomStep);
70 }
71
72 function doZoomDefault() {
73 chrome.tabs.setZoom(1.0, function() {
74 if (chrome.runtime.lastError)
75 console.log('[ZoomDemoExtension] ' + chrome.runtime.lastError.message);
76 });
77 }
78
79 function doSetMode() {
80 var modeVal;
81 var modeRadios = document.getElementsByName('modeRadio');
82 for (var i = 0; i < modeRadios.length; i++) {
83 if (modeRadios[i].checked)
84 modeVal = modeRadios[i].value;
85 }
86
87 var scopeVal;
88 var scopeRadios = document.getElementsByName('scopeRadio');
89 for (var i = 0; i < scopeRadios.length; i++) {
90 if (scopeRadios[i].checked)
91 scopeVal = scopeRadios[i].value;
92 }
93
94 if (!modeVal || !scopeVal) {
95 console.log(
96 '[ZoomDemoExtension] Must specify values for both mode & scope.');
97 return;
98 }
99
100 chrome.tabs.setZoomSettings({ mode: modeVal, scope: scopeVal },
101 function() {
102 if (chrome.runtime.lastError) {
103 console.log('[ZoomDemoExtension] doSetMode() error: ' +
104 chrome.runtime.lastError.message);
105 }
106 });
107 }
108
109 function doClose() {
110 self.close();
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698