-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgame_js.py
141 lines (117 loc) · 4.46 KB
/
game_js.py
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
js_script = """
<script>
Array.from(window.parent.document.querySelectorAll('button[kind=secondary]')).find(el => el.innerText === 'L').classList.add('bbutton-left');
Array.from(window.parent.document.querySelectorAll('button[kind=secondary]')).find(el => el.innerText === 'R').classList.add('bbutton-right');
Array.from(window.parent.document.querySelectorAll('button[kind=secondary]')).find(el => el.innerText === 'U').classList.add('bbutton-up');
Array.from(window.parent.document.querySelectorAll('button[kind=secondary]')).find(el => el.innerText === 'D').classList.add('bbutton-down');
const doc = window.parent.document;
buttons = Array.from(doc.querySelectorAll('button[kind=secondary]'));
const left_button = buttons.find(el => el.innerText === 'LEFT');
const right_button = buttons.find(el => el.innerText === 'RIGHT');
const up_button = buttons.find(el => el.innerText === 'UP');
const down_button = buttons.find(el => el.innerText === 'DOWN');
const left_button2 = buttons.find(el => el.innerText === 'L');
const right_button2 = buttons.find(el => el.innerText === 'R');
const up_button2 = buttons.find(el => el.innerText === 'U');
const down_button2 = buttons.find(el => el.innerText === 'D');
doc.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 37: // (37 = left arrow)
left_button.click();
window.parent.document.getElementById('player').scrollIntoView();
break;
case 39: // (39 = right arrow)
right_button.click();
window.parent.document.getElementById('player').scrollIntoView();
break;
case 38: // (39 = right arrow)
up_button.click();
window.parent.document.getElementById('player').scrollIntoView();
break;
case 40: // (39 = right arrow)
down_button.click();
window.parent.document.getElementById('player').scrollIntoView();
break;
}
});
left_button.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("left")
});
right_button.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("right")
});
left_button2.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("left")
});
right_button2.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("right")
});
up_button.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("up")
});
down_button.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("down")
});
up_button2.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("up")
});
down_button2.addEventListener("click",function() {
window.parent.document.getElementById('player').scrollIntoView();
console.log("down")
});
</script>
"""
js_script_optimized = """<script>
const doc = window.parent.document;
const buttons = Array.from(doc.querySelectorAll('button[kind=secondary]'));
function addButtonClass(button, className) {
button.classList.add(className);
}
function scrollPlayerIntoView() {
doc.getElementById('player').scrollIntoView();
}
const buttonMapping = {
'L': 'bbutton-left',
'R': 'bbutton-right',
'U': 'bbutton-up',
'D': 'bbutton-down',
'LEFT': 'left_button',
'RIGHT': 'right_button',
'UP': 'up_button',
'DOWN': 'down_button',
};
const buttonRefs = {};
buttons.forEach((button) => {
const className = buttonMapping[button.innerText];
if (className) {
addButtonClass(button, className);
buttonRefs[className] = button;
}
});
doc.addEventListener('keydown', (e) => {
const keyCodeMapping = {
37: buttonRefs.left_button,
38: buttonRefs.up_button,
39: buttonRefs.right_button,
40: buttonRefs.down_button,
};
const button = keyCodeMapping[e.keyCode];
if (button) {
button.click();
scrollPlayerIntoView();
}
});
Object.values(buttonRefs).forEach((button) => {
button.addEventListener('click', () => {
scrollPlayerIntoView();
console.log(button.innerText.toLowerCase());
});
});
</script>"""