forked from mrlionovsky/flashplayer-for-spaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (140 loc) · 6.83 KB
/
main.js
File metadata and controls
167 lines (140 loc) · 6.83 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
// ==UserScript==
// @name Ruffle Flash Player for Spaces.im Files
// @namespace http://tampermonkey.net/
// @version 1.9
// @description Добавляет поддержку flash на Spaces.
// @author Lionovsky
// @match *://spaces.im/*
// @match *://*.spcs.bio/*
// @grant GM_xmlhttpRequest
// @connect *
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// ------------------------------------------
// 1. ФУНКЦИЯ ДЛЯ ОБРАБОТКИ AJAX-НАВИГАЦИИ
// ------------------------------------------
function observeUrlChanges() {
const originalPush = history.pushState;
const originalReplace = history.replaceState;
history.pushState = function() {
originalPush.apply(history, arguments);
window.dispatchEvent(new Event('pushstate'));
};
history.replaceState = function() {
originalReplace.apply(history, arguments);
window.dispatchEvent(new Event('replacestate'));
};
window.addEventListener('popstate', embedRufflePlayer);
window.addEventListener('pushstate', embedRufflePlayer);
window.addEventListener('replacestate', embedRufflePlayer);
}
// ------------------------------------------
// 2. ГЛАВНАЯ ФУНКЦИЯ ВСТРАИВАНИЯ ПЛЕЕРА
// ------------------------------------------
let observer = null;
function embedRufflePlayer() {
if (!/spaces\.im\/files\/view\/|spcs\.bio/i.test(window.location.href)) {
if (observer) {
observer.disconnect();
observer = null;
}
return;
}
const downloadLink = document.querySelector('a[href$=".swf"]:not([data-ruffle-embedded])');
if (downloadLink) {
if (observer) {
observer.disconnect();
observer = null;
}
downloadLink.setAttribute('data-ruffle-embedded', 'true');
const swfUrl = downloadLink.href;
if (typeof GM_xmlhttpRequest !== 'function') {
console.error('[Ruffle UserScript] GM_xmlhttpRequest недоступен.');
return;
}
const insertionPoint = downloadLink.closest('.file-action-bar') || downloadLink.parentNode;
downloadLink.style.display = 'none';
// --- 2. Вставляем ЯРКИЙ Индикатор Загрузки ---
const loadingIndicator = document.createElement('div');
loadingIndicator.innerHTML = `
<div style="
padding: 20px;
text-align: center;
border: 2px solid #ff9800;
background: #ff9800;
color: white;
font-size: 16px;
font-weight: bold;
margin: 20px 0;
border-radius: 5px;
">
⏳ Идет загрузка Flash-файла (SWF) через Ruffle... <br>
<span style="font-size: 14px; font-weight: normal; display: block; margin-top: 5px;">
Пожалуйста, подождите, это может занять несколько секунд, пока обходим ограничения.
</span>
</div>
`;
loadingIndicator.id = 'ruffle-loading-indicator';
insertionPoint.parentNode.insertBefore(loadingIndicator, insertionPoint);
// 3. Используем GM_xmlhttpRequest для обхода CORS
GM_xmlhttpRequest({
method: "GET",
url: swfUrl,
responseType: "arraybuffer",
onload: function(response) {
loadingIndicator.remove();
if (response.status === 200) {
try {
const swfBlob = new Blob([response.response], { type: 'application/x-shockwave-flash' });
const blobUrl = URL.createObjectURL(swfBlob);
// --- 4. Создание и встраивание элементов плеера ---
const flashPlayerContainer = document.createElement('div');
flashPlayerContainer.style.textAlign = 'center';
flashPlayerContainer.style.marginBottom = '20px';
flashPlayerContainer.style.marginTop = '20px';
flashPlayerContainer.style.backgroundColor = '#000000';
const flashPlayer = document.createElement('object');
flashPlayer.setAttribute('data', blobUrl);
flashPlayer.setAttribute('type', 'application/x-shockwave-flash');
flashPlayer.style.width = '100%';
flashPlayer.style.maxWidth = '600px';
flashPlayer.style.height = '400px';
flashPlayerContainer.appendChild(flashPlayer);
insertionPoint.parentNode.insertBefore(flashPlayerContainer, insertionPoint);
window.addEventListener('unload', () => URL.revokeObjectURL(blobUrl));
} catch (e) {
console.error('[Ruffle UserScript] Ошибка создания Blob или встраивания плеера:', e);
downloadLink.style.display = '';
}
} else {
console.error(`[Ruffle UserScript] Не удалось загрузить SWF. Статус: ${response.status}`);
downloadLink.style.display = '';
}
},
onerror: function(error) {
console.error('[Ruffle UserScript] Ошибка GM_xmlhttpRequest:', error);
loadingIndicator.remove();
downloadLink.style.display = '';
}
});
} else {
// Если ссылка не найдена СРАЗУ, начинаем наблюдение за DOM
if (!observer) {
observer = new MutationObserver(function(mutations, observer) {
embedRufflePlayer();
});
observer.observe(document.body || document.documentElement, {
childList: true,
subtree: true
});
}
}
}
// ------------------------------------------
// 3. ЗАПУСК
// ------------------------------------------
observeUrlChanges();
embedRufflePlayer();
})();