forked from Yash-Singh1/zoom.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
190 lines (175 loc) · 4.62 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @file The main source code for zoom.js
* @version 1.1.1
* @author Yash Singh
* @license MIT
*/
zoom = {};
/**
* The name of the program => zoom.js
* @license MIT
* @type {string}
* @const
*/
zoom.name = "zoom.js";
/**
* The current version
* @license MIT
* @type {string}
* @constant
*/
zoom.version = "1.1.1";
/**
* The license used => MIT
* @license MIT
* @type {string}
* @constant
*/
zoom.license = "MIT";
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
document.body.style["-moz-transform"] = "scale(1)";
document.body.style["-moz-transform-origin"] = "0 0";
document.body.style.zoom = 1;
} else {
if (typeof window.onload !== "function") {
window.onload = function () {};
}
window.onload = (function () {
var cached_function = window.onload;
return function () {
var result = cached_function.apply(this, arguments);
document.body.style["-moz-transform"] = "scale(1)";
document.body.style["-moz-transform-origin"] = "0 0";
document.body.style.zoom = 1;
if (typeof webFrame !== "undefined") {
webFrame.setZoomFactor(1);
}
return result;
};
})();
}
/**
* A function to zoom in and out cross-browser
* @param {number} int The scale at which to zoom in or out. The smaller the more it zooms out.
* @returns {number} The current zoom value
* @license MIT
*/
zoom.zoom = function (int) {
return this.specify(function (givenInteger) {
return givenInteger * int;
}, int);
};
/**
* Get the current zoom of the page
* @param {boolean} [bool] console.logs the current zoom of the page
* @returns {number} The current zoom value
* @license MIT
*/
zoom.get = function (bool) {
let currentZoom;
if (typeof webFrame !== "undefined") {
currentZoom = webFrame.getZoomFactor();
} else {
currentZoom = parseFloat(document.body.style.zoom);
}
if (bool == true) {
console.log(currentZoom);
}
return currentZoom;
};
/**
* Specify a function given data and the zoom value to set the zoom
* @param {function} specifiedFunction A function given the current zoom as the first argument and the data parameter as the second argument. The returned value will be used for the zoom
* @param {any} [data] Will be passed into the specifiedFunction as the second argument
* @returns {number} The current zoom value
* @license MIT
*/
zoom.specify = function (specifiedFunction, data) {
if (typeof webFrame !== "undefined") {
webFrame.setZoomFactor(specifiedFunction(webFrame.getZoomFactor(), data));
} else {
document.body.style["-moz-transform"] =
"scale(" +
specifiedFunction(
parseFloat(
document.body.style["-moz-transform"].substring(
6,
document.body.style["-moz-transform"].length - 1
)
),
data
) +
")";
}
document.body.style.zoom = specifiedFunction(zoom.get(), data);
return zoom.get();
};
/**
* Additive into the zoom
* @param {number} int The amount in which to zoom in and out.
* @returns {number} The current zoom value
* @license MIT
*/
zoom.add = function (int) {
return this.specify(function (givenInteger) {
return givenInteger + int;
}, int);
};
/**
* Allows you to set the zoom value to a number
* @param {number} int The integer to set the zoom to
* @returns {number} The current zoom value
* @license MIT
*/
zoom.set = function (int) {
return this.specify(function (givenInteger, data) {
return data;
}, int);
};
/**
* Opposite of zoom.add, remove from the zoom value
* @param {number} int The amount in which to zoom in and out. Opposite of zoom.add
* @returns {number} The current zoom value
* @license MIT
*/
zoom.minus = function (int) {
return this.add(-1 * int);
};
/**
* Invert the current zoom value
* @returns {number} The current zoom value
* @license MIT
*/
zoom.invert = function () {
return this.specify(function (givenInteger) {
return 1 / givenInteger;
});
};
/**
* Opposite of zoom.zppm, divide the current zoom value
* @param {number} int The amount in which to zoom in and out. Opposite of zoom.zoom
* @returns {number} The current zoom value
* @license MIT
*/
zoom.inverseZoom = function (int) {
return this.specify(function (givenInteger) {
return givenInteger / int;
}, int);
};
/**
* Reset the zoom value to original
* @returns {number} The current zoom value
* @license MIT
*/
zoom.reset = function () {
document.body.style["-moz-transform"] = "scale(1)";
document.body.style["-moz-transform-origin"] = "0 0";
document.body.style.zoom = 1;
if (typeof webFrame !== "undefined") {
webFrame.setZoomFactor(1);
}
return 1;
};