Repository navigation
Expand file tree
/
Copy pathuserscript.js
More file actions
214 lines (183 loc) · 7.68 KB
/
Copy pathuserscript.js
File metadata and controls
214 lines (183 loc) · 7.68 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(function() {
'use strict';
/* -----------------------------
Reload watcher
------------------------------*/
function base64ToUint8Array(base64) {
console.log("converting to uint8array");
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
let lastPath = null;
let projdata = null;
let polling = false;
let initPoll = true;
async function pollBlockVine() {
if (polling) return;
polling = true;
try {
const res = await fetch('http://localhost:8617/cmd/getInfo');
if (!res.ok) return;
const info = await res.json();
const path = String(info.path ?? '');
const hasReload = info.action_queue?.at(-1) === 'reload';
const shouldReload =
hasReload ||
path !== lastPath;
if (shouldReload && typeof vm !== 'undefined' && info.projdata) {
if (path !== lastPath && !initPoll) {
EditorPreload.openNewWindow();
document.body.innerHTML = '🌱 Please close this window.';
requestAnimationFrame(() => {
requestAnimationFrame(() => {
window.close();
});
});
}
lastPath = path;
const projdata = base64ToUint8Array(info.projdata);
console.log("reloading!");
await vm.loadProject(projdata);
}
} finally {
polling = false;
initPoll = false;
}
}
setInterval(pollBlockVine, 1000);
/* -----------------------------
UI integration
------------------------------*/
function queryByClassPrefix(prefix) {
return Array.from(document.querySelectorAll('div'))
.find(el => Array.from(el.classList).some(c => c.startsWith(prefix)));
}
function addMenuItem() {
const menuGroup = queryByClassPrefix('menu-bar_file-group');
if (!menuGroup) return false;
if (queryByClassPrefix('custom-test-item')) return true;
const newItem = document.createElement('div');
newItem.className = 'custom-test-item';
newItem.classList.add(
...Array.from(menuGroup.querySelectorAll('div'))
.map(el =>
Array.from(el.classList).filter(
c =>
c.startsWith('menu-bar_menu-bar-item') ||
c.startsWith('menu-bar_hoverable')
)
)
.flat()
);
newItem.textContent = '🌱 BlockVine (Git)';
newItem.addEventListener('click', () => {
fetch('http://localhost:8617', { method: 'GET' })
.then(response => {
if (!response.ok) {
alert(
'Failed to connect to the BlockVine local backend. Please re-open BlockVine and try again.'
);
return;
}
if (document.querySelector('#blockvine-sidebar')) return;
const sidebar = document.createElement('div');
sidebar.id = 'blockvine-sidebar';
Object.assign(sidebar.style, {
position: 'fixed',
top: '0',
left: '0',
width: '360px',
height: '100%',
background: '#1e1e1e',
borderRight: '2px solid #444',
boxShadow: '4px 0 24px rgba(0,0,0,0.5)',
zIndex: 9999,
display: 'flex',
flexDirection: 'column',
transition: 'transform 0.25s ease, left 0.25s ease, right 0.25s ease'
});
const titleBar = document.createElement('div');
Object.assign(titleBar.style, {
background: '#333',
color: '#fff',
padding: '12px 12px',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
fontWeight: 'normal'
});
titleBar.textContent = '🌱 Git';
const closeBtn = document.createElement('button');
closeBtn.textContent = '×';
Object.assign(closeBtn.style, {
background: '#222',
border: 'none',
borderRadius: '1em',
color: '#fff',
fontSize: '20px',
cursor: 'pointer',
fontWeight: 'bold'
});
closeBtn.onclick = () => sidebar.remove();
const dockBtn = document.createElement('button');
dockBtn.textContent = '⇄';
Object.assign(dockBtn.style, {
background: '#222',
border: 'none',
borderRadius: '1em',
color: '#fff',
fontSize: '16px',
cursor: 'pointer',
fontWeight: 'bold',
marginRight: '8px'
});
let dockedLeft = true;
dockBtn.onclick = () => {
if (dockedLeft) {
sidebar.style.left = 'auto';
sidebar.style.right = '0';
sidebar.style.borderRight = 'none';
sidebar.style.borderLeft = '2px solid #444';
} else {
sidebar.style.right = 'auto';
sidebar.style.left = '0';
sidebar.style.borderLeft = 'none';
sidebar.style.borderRight = '2px solid #444';
}
dockedLeft = !dockedLeft;
};
const buttonContainer = document.createElement('div');
buttonContainer.style.display = 'flex';
buttonContainer.appendChild(dockBtn);
buttonContainer.appendChild(closeBtn);
titleBar.appendChild(buttonContainer);
sidebar.appendChild(titleBar);
const iframe = document.createElement('iframe');
iframe.src = 'http://localhost:8617/gui';
Object.assign(iframe.style, {
flex: '1',
border: 'none',
width: '100%'
});
sidebar.appendChild(iframe);
document.body.appendChild(sidebar);
})
.catch(() => {
alert(
'TurboWarp was not launched alongside BlockVine. Please open BlockVine from your app menu.'
);
});
});
menuGroup.appendChild(newItem);
return true;
}
const observer = new MutationObserver(() => {
if (addMenuItem()) observer.disconnect();
});
observer.observe(document.body, { childList: true, subtree: true });
addMenuItem();
})();