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