| OLD | NEW |
| (Empty) | |
| 1 <script> |
| 2 |
| 3 // The zoom extension API only allows setting of an arbitrary zoom percent. |
| 4 // The following functions provide the logic necessary to imitate the exact |
| 5 // zoom in/out behavior of the built-in zoom functions. |
| 6 |
| 7 // From WebViewImpl.cpp |
| 8 |
| 9 var textSizeMultiplierRatio = 1.2; |
| 10 |
| 11 function zoomLevelToZoomFactor(zoomLevel) { |
| 12 return Math.pow(textSizeMultiplierRatio, zoomLevel); |
| 13 } |
| 14 |
| 15 function zoomFactorToZoomLevel(factor) { |
| 16 // Since factor = 1.2^level, level = log(factor) / log(1.2) |
| 17 return Math.log(factor) / Math.log(textSizeMultiplierRatio); |
| 18 } |
| 19 |
| 20 // From render_view.cc |
| 21 |
| 22 var ZoomFunction = { |
| 23 ZoomOut: -1, |
| 24 Reset: 0, |
| 25 ZoomIn: 1 |
| 26 } |
| 27 |
| 28 // Test that a float reperesents an integral zoom level |
| 29 function isBasicallyIntegral(zoomLevel) { |
| 30 var epsilon = 0.000001; |
| 31 var rounded = Math.round(zoomLevel); |
| 32 return Math.abs(zoomLevel - rounded) < epsilon; |
| 33 } |
| 34 |
| 35 // Calculate a new zoom level for zoom in/out/reset |
| 36 function zoom(oldZoomLevel, zoomFunction) { |
| 37 var zoomLevel; |
| 38 |
| 39 if (zoomFunction == ZoomFunction.Reset) { |
| 40 zoomLevel = 0; |
| 41 } else if (isBasicallyIntegral(oldZoomLevel)) { |
| 42 // Previous zoom level is a whole number, so just increment/decrement. |
| 43 zoomLevel = Math.round(oldZoomLevel) + zoomFunction; |
| 44 } else { |
| 45 // Either the user hit the zoom factor limit and thus the zoom level is now |
| 46 // not a whole number, or a plugin changed it to a custom value. We want |
| 47 // to go to the next whole number so that the user can always get back to |
| 48 // 100% with the keyboard/menu. |
| 49 if ((oldZoomLevel > 1 && zoomFunction > 0) || |
| 50 (oldZoomLevel < 1 && zoomFunction < 0)) { |
| 51 zoomLevel = parseInt(oldZoomLevel + zoomFunction); |
| 52 } else { |
| 53 // We're going towards 100%, so first go to the next whole number. |
| 54 zoomLevel = parseInt(oldZoomLevel); |
| 55 } |
| 56 } |
| 57 |
| 58 return zoomLevel; |
| 59 } |
| 60 |
| 61 |
| 62 // Zoom in/out |
| 63 |
| 64 function incZoomLevel(oldZoomLevel) { |
| 65 return zoom(oldZoomLevel, ZoomFunction.ZoomIn); |
| 66 } |
| 67 |
| 68 function decZoomLevel(oldZoomLevel) { |
| 69 return zoom(oldZoomLevel, ZoomFunction.ZoomOut); |
| 70 } |
| 71 |
| 72 // Convert zoom percent to zoom level and apply a function, |
| 73 // then convert back to zoom percent and return the result |
| 74 function applyAsZoomLevel(zoomLevelTransform, zoomPercent) { |
| 75 var zoomLevel = zoomFactorToZoomLevel(zoomPercent / 100); |
| 76 var newZoomLevel = zoomLevelTransform(zoomLevel); |
| 77 var newZoomPercent = zoomLevelToZoomFactor(newZoomLevel) * 100; |
| 78 return newZoomPercent; |
| 79 } |
| 80 |
| 81 // Zoom in the current tab, exactly like the built-in zoom |
| 82 function zoomIn() { |
| 83 chrome.tabs.getSelected(null, function(tab) { |
| 84 chrome.tabs.getZoomPercent(tab.id, function(zoomPercent) { |
| 85 |
| 86 var newZoomPercent = applyAsZoomLevel(incZoomLevel, zoomPercent); |
| 87 chrome.tabs.setZoomPercent(tab.id, newZoomPercent, function(finalZoomP
ercent) { |
| 88 displayZoomPercent(finalZoomPercent); |
| 89 }); |
| 90 }); |
| 91 }); |
| 92 } |
| 93 |
| 94 // Zoom out the current tab, exactly like the built-in zoom |
| 95 function zoomOut() { |
| 96 chrome.tabs.getSelected(null, function(tab) { |
| 97 chrome.tabs.getZoomPercent(tab.id, function(zoomPercent) { |
| 98 |
| 99 var newZoomPercent = applyAsZoomLevel(decZoomLevel, zoomPercent); |
| 100 chrome.tabs.setZoomPercent(tab.id, newZoomPercent, function(finalZoomP
ercent) { |
| 101 displayZoomPercent(finalZoomPercent); |
| 102 }); |
| 103 }); |
| 104 }); |
| 105 } |
| 106 |
| 107 function zoomReset() { |
| 108 chrome.tabs.getSelected(null, function(tab) { |
| 109 chrome.tabs.setZoomPercent(tab.id, 100, function(finalZoomPercent) { |
| 110 displayZoomPercent(finalZoomPercent); |
| 111 }); |
| 112 }); |
| 113 } |
| 114 |
| 115 function setZoomPercentFromInput() { |
| 116 chrome.tabs.getSelected(null, function(tab){ |
| 117 var zoomPercent = parseFloat(document.getElementById("zoomPercentInput").v
alue); |
| 118 |
| 119 if (isNaN(zoomPercent)) { |
| 120 return; |
| 121 } |
| 122 |
| 123 chrome.tabs.setZoomPercent(tab.id, zoomPercent, function(finalZoomPercent)
{ |
| 124 displayZoomPercent(finalZoomPercent); |
| 125 }); |
| 126 }); |
| 127 } |
| 128 |
| 129 function displayZoomPercent(zoomPercent) { |
| 130 var intZoomPercent = parseInt(zoomPercent); |
| 131 document.getElementById("currentZoom").innerHTML = intZoomPercent; |
| 132 } |
| 133 |
| 134 function getAndDisplayZoomPercent() { |
| 135 chrome.tabs.getSelected(null, function(tab){ |
| 136 chrome.tabs.getZoomPercent(tab.id, function(zoomPercent) { |
| 137 displayZoomPercent(zoomPercent); |
| 138 }); |
| 139 }); |
| 140 } |
| 141 |
| 142 getAndDisplayZoomPercent(); |
| 143 |
| 144 </script> |
| 145 |
| 146 <p><a id="zoomInLink" href="#" onclick="zoomIn()">zoom in</a></p> |
| 147 <p><a id="zoomOutLink" href="#" onclick="zoomOut()">zoom out</a></p> |
| 148 <p><a id="zoomResetLink" href="#" onClick="zoomReset()">reset</a></p> |
| 149 <p>Zoom: <span id="currentZoom"></span></p> |
| 150 |
| 151 <form> |
| 152 <input id="zoomPercentInput"/> |
| 153 <button id="zoomPercentButton" onclick="setZoomPercentFromInput()" type="butto
n">Set Zoom</button> |
| 154 </form> |
| OLD | NEW |