deevroman / better-osm-org

A userscript that adds several useful features to osm.org. β
25 stars 1 forks source link

Working with the map is not supported in Violentmonkey #34

Closed deevroman closed 2 weeks ago

deevroman commented 2 weeks ago

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 use unsafeWindow.map.*

However, GM_addElement does not bypass the CSP of the osm.org site

Judging 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 in unsafeWindow

// ==UserScript==
// @name         Better osm.org
// @version      0.4.1
// @author       deevroman
// @match        https://www.openstreetmap.org/*
// @sandbox      JavaScript
// @grant        none
// @run-at       document-end
// ==/UserScript==

console.log("start")
const kek = function (){
  debugger
  alert("lol")
}
debugger

// unsafeWindow.kek = cloneInto(kek, unsafeWindow, {cloneFunctions: true})
// unsafeWindow.kek = exportFunction(kek, unsafeWindow);

GM_addElement("script", {
    textContent: `
        var map = null;
        console.log("start map intercepted")
        L.Map.addInitHook((function () {
                if (this._container?.id === "map") {
                    map = this;
                    console.log("map intercepted")
                }
            })
        )`
})
console.log("end")

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 with cloneInto/exportFunction doesn’t work

deevroman commented 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")
deevroman commented 2 weeks ago

Okay, Violentmonkey support has been saved