Files
ogame_db/ogame-importer/inject.js
T
2026-09-23 18:59:27 +02:00

60 lines
2.0 KiB
JavaScript

/* OGame Spy Auto-Importer — script MAIN world (iniettato nella pagina)
*
* Ruolo: intercettare il testo che la pagina OGame COPIA negli appunti,
* senza mai doverli leggere. Questo evita del tutto il problema dei permessi
* clipboard nei content script (dove `navigator.clipboard.readText()` è
* soggetta ai permessi della pagina e viene rifiutata → auto-import muto).
*
* Copre due strade:
* 1) navigator.clipboard.writeText() → pulsante "API"/"Copia" di OGame
* 2) l'evento DOM 'copy' → Ctrl+C / tasto destro / execCommand
* (quest'ultimo è gestito direttamente da content.js, in isolated world)
*
* Gira a document_start per agganciare writeText prima degli script di gioco.
*/
(function () {
'use strict';
const CHANNEL = 'ogame-spy-importer';
let hooked = false;
function share(text) {
if (!text || typeof text !== 'string') return;
try {
window.postMessage({ __ogameSpyImporter: CHANNEL, text: text }, '*');
} catch (_) { /* noop */ }
}
function hookWriteText(target) {
if (!target || typeof target.writeText !== 'function' || target.writeText.__spyHooked) return false;
const orig = target.writeText;
const wrapper = function (text) {
share(text);
return orig.apply(this, arguments);
};
try {
wrapper.__spyHooked = true;
target.writeText = wrapper;
return true;
} catch (_) {
// proprietà non scrivibile: prova a definire una own property
try {
Object.defineProperty(target, 'writeText', {
configurable: true, writable: true, value: wrapper,
});
return true;
} catch (_) {
return false;
}
}
}
try {
// Istanza (shadows il prototipo) + prototipo, per essere sicuri.
hooked = hookWriteText(navigator.clipboard) || hooked;
if (window.Clipboard && Clipboard.prototype) {
hooked = hookWriteText(Clipboard.prototype) || hooked;
}
} catch (_) { /* noop */ }
})();