forked from ScratchAddons/ScratchAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
82 lines (74 loc) · 2.78 KB
/
Copy pathuserscript.js
File metadata and controls
82 lines (74 loc) · 2.78 KB
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
export default async function ({ addon, console }) {
// TODO: test whether e.altKey is true in chromebooks when alt+clicking.
// If so, no timeout needed, similar to mute-project addon.
let global_fps = 30;
const vm = addon.tab.traps.vm;
let mode = false;
let monitorUpdateFixed = false;
const fastFlag = addon.self.dir + "/svg/fast-flag.svg";
let vanillaFlag = null;
while (true) {
let button = await addon.tab.waitForElement("[class^='green-flag_green-flag']", {
markAsSeen: true,
reduxEvents: ["scratch-gui/mode/SET_PLAYER", "fontsLoaded/SET_FONTS_LOADED", "scratch-gui/locales/SELECT_LOCALE"],
});
const updateFlag = () => {
if (!vanillaFlag) vanillaFlag = button.src;
button.src = mode ? fastFlag : vanillaFlag;
};
const changeMode = (_mode = !mode) => {
mode = _mode;
if (mode) {
setFPS(addon.settings.get("framerate"));
// monitor updates are throttled by default
// https://github.com/scratchfoundation/scratch-gui/blob/ba76db7/src/reducers/monitors.js
if (!monitorUpdateFixed) {
const originalListener = vm.listeners("MONITORS_UPDATE").find((f) => f.name === "onMonitorsUpdate");
if (originalListener) vm.removeListener("MONITORS_UPDATE", originalListener);
vm.on("MONITORS_UPDATE", (monitors) =>
addon.tab.redux.dispatch({
type: "scratch-gui/monitors/UPDATE_MONITORS",
monitors,
})
);
monitorUpdateFixed = true;
}
} else setFPS(30);
updateFlag();
};
const flagListener = (e) => {
if (addon.self.disabled) return;
const isAltClick = e.type === "click" && e.altKey;
const isChromebookAltClick = navigator.userAgent.includes("CrOS") && e.type === "contextmenu";
if (isAltClick || isChromebookAltClick) {
e.cancelBubble = true;
e.preventDefault();
changeMode();
}
};
button.addEventListener("click", flagListener);
button.addEventListener("contextmenu", flagListener);
const setFPS = (fps) => {
global_fps = addon.self.disabled ? 30 : fps;
clearInterval(vm.runtime._steppingInterval);
vm.runtime._steppingInterval = null;
vm.runtime.start();
};
addon.settings.addEventListener("change", () => {
if (vm.runtime._steppingInterval) {
setFPS(addon.settings.get("framerate"));
}
});
addon.self.addEventListener("disabled", () => changeMode(false));
vm.runtime.start = function () {
if (this._steppingInterval) return;
let interval = 1000 / global_fps;
this.currentStepTime = interval;
this._steppingInterval = setInterval(() => {
this._step();
}, interval);
this.emit("RUNTIME_STARTED");
};
updateFlag();
}
}