-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathloop-through-widths.js
86 lines (71 loc) · 1.89 KB
/
loop-through-widths.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
// https://github.com/hchiam/learning-js/blob/master/bookmarklets/autoResizeWindowToTestMediaQueries.js
export const commands = [
'loop through widths',
'loop thru widths',
]
export const description = 'loops through screen widths in a popup'
export default async function() {
const test = (function () {
let keepGoing = true;
let popup;
const maxWidth = screen.width;
const minWidth = 152;
let goWider = true;
openPopup();
go();
popup.onbeforeunload = function () {
stop();
console.log("Stopped timer.");
};
function openPopup() {
popup = window.open(location.href, "_blank", "width=100, top=0, left=0");
}
function wider() {
popup.resizeBy(5, 0);
}
function thinner() {
popup.resizeBy(-5, 0);
}
function stop() {
keepGoing = false;
}
function go() {
keepGoing = true;
console.log("Popup will start looping thru screen widths in 3 seconds.");
popup.focus();
setTimeout(function () {
scanWidths();
const codeStyle = 'background: black; color: lime;'
const resetStyle = 'background: inherit; color: inherit;'
console.log(
"You can run %ctest.stop()%c to stop, and \n%ctest.go()%c to continue.",
codeStyle, resetStyle, codeStyle, resetStyle
);
}, 3000);
}
function scanWidths() {
const timer = setInterval(function () {
if (!keepGoing) {
clearTimeout(timer);
popup.focus();
}
if (maxWidth <= popup.outerWidth) {
goWider = false;
} else if (popup.outerWidth <= minWidth) {
goWider = true;
}
if (goWider) {
wider();
} else {
thinner();
}
}, 100);
}
return {
stop,
go,
popup
};
})();
if (typeof window !== "undefined") window.test = test;
}