Closed deevroman closed 2 weeks ago
Fucking browsers
Solution for Firefox:
// ==UserScript==
// @name Better osm.org
// @version 0.4.1
// @author deevroman
// @match https://www.openstreetmap.org/*
// @namespace https://github.com/deevroman/better-osm-org
// @sandbox JavaScript
// @grant GM_info
// @run-at document-end
// ==/UserScript==
console.log("start")
function kek(){
console.log("start map intercepteing")
window.wrappedJSObject.L.Map.addInitHook(exportFunction((function () {
if (this._container?.id === "map") {
window.wrappedJSObject.globalThis.map = this;
console.log("map intercepted");
}
}), window.wrappedJSObject)
)
}
window.wrappedJSObject.kek = exportFunction(kek, window.wrappedJSObject)
window.wrappedJSObject.kek()
setTimeout(() => {
console.log(window.wrappedJSObject.map.getBounds())
}, 2000)
console.log("end")
Solution for Chrome:
// ==UserScript==
// @name Better osm.org
// @version 0.4.1
// @author deevroman
// @match https://www.openstreetmap.org/*
// @namespace https://github.com/deevroman/better-osm-org
// @sandbox JavaScript
// @grant GM_info
// @run-at document-end
// ==/UserScript==
console.log("start")
function kek(){
console.log("start map intercepteing")
unsafeWindow.L.Map.addInitHook(exportFunction((function () {
if (this._container?.id === "map") {
unsafeWindow.map = this;
console.log("map intercepted");
}
}), unsafeWindow) //
)
}
unsafeWindow.kek = exportFunction(kek, unsafeWindow)
unsafeWindow.kek()
setTimeout(() => {
console.log(unsafeWindow.map.getBounds())
}, 2000)
console.log("end")
Okay, Violentmonkey support has been saved
The main thing on which the ability to work with a map rests is
L.Map.addInitHook
, which pulls the map object into the global scope. After this it becomes possible to useunsafeWindow.map.*
However,
GM_addElement
does not bypass the CSP of the osm.org siteJudging by https://github.com/violentmonkey/violentmonkey/issues/1001#issuecomment-2308438866 something like
cloneInto
exportFunction
should work, after which you can get the coveted map object inunsafeWindow
Judging by https://github.com/violentmonkey/violentmonkey/issues/1001#issuecomment-640223982 Tampermonkey does not do very good with page security, so the ViolentMonkey developers are in no hurry to implement such a mechanism.
I understand why this places
GM_addElement
, but I don’t understand why the solution withcloneInto
/exportFunction
doesn’t work