From 63af8b25c936fb6f521477c0cff986c3e8a8ce72 Mon Sep 17 00:00:00 2001 From: Zafrir Ron Date: Sun, 8 Oct 2023 00:37:15 +0300 Subject: [PATCH] add code inject option (#205) --- worldmap/worldmap.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/worldmap/worldmap.js b/worldmap/worldmap.js index 26e3b79..12a50f7 100644 --- a/worldmap/worldmap.js +++ b/worldmap/worldmap.js @@ -54,6 +54,32 @@ var iconSz = { "Command": 44 }; +var filesAdded = ''; + +var loadStatic = function(fileName){ + if(filesAdded.indexOf(fileName) !== -1) + return + var head = document.getElementsByTagName('head')[0] + if(fileName.indexOf('js') !== -1) { + var script = document.createElement('script') + script.src = fileName + script.type = 'text/javascript' + console.log("Loading: ",fileName) + head.append(script) + filesAdded += ' ' + fileName + } else if (fileName.indexOf('css') !== -1) { + var style = document.createElement('link') + style.href = fileName + style.type = 'text/css' + style.rel = 'stylesheet' + console.log("Loading: ",fileName) + head.append(style); + filesAdded += ' ' + fileName + } else { + console.log("Unsupported file type: ",fileName) + } +} + // L.PM.setOptIn(true); // Create the socket @@ -2899,6 +2925,19 @@ function doCommand(cmd) { } } } + if (cmd.hasOwnProperty("loadStatic")) { + console.log("load Static files",JSON.stringify(cmd.loadStatic.fileNames)); + cmd.loadStatic.fileNames.forEach(fileName => loadStatic(fileName)); + } + if (cmd.hasOwnProperty("customCmd")) { + console.log("custom Command",JSON.stringify(cmd.customCmd)); + try { + eval(cmd.customCmd); + } + catch (e) { + console.log("ERR - custom command: ", JSON.stringify(cmd.customCmd)); + } + } } // handle any incoming GEOJSON directly - may style badly