diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index 25a7101ff..4425348ca 100644 --- a/plugins/draw-tools.user.js +++ b/plugins/draw-tools.user.js @@ -476,7 +476,20 @@ window.plugin.drawTools.optPaste = function() { } else { - var data = JSON.parse(promptAction); + var data; + try{ + data = JSON.parse(promptAction); + } catch(e) { + // invalid json, filter out any leading or trailing text and try again + var mutatedPromptAction = promptAction; + if(!mutatedPromptAction.match(new RegExp('^\\[\\{'))) { + mutatedPromptAction = mutatedPromptAction.slice(mutatedPromptAction.indexOf('[{')); + } + if(!mutatedPromptAction.match(new RegExp('\\}\\]$'))) { + mutatedPromptAction = mutatedPromptAction.slice(0, mutatedPromptAction.lastIndexOf('}]') + 2); + } + data = JSON.parse(mutatedPromptAction); // throws a new exception if we still didn't get good data, which is handled by the outer enclosure + } window.plugin.drawTools.drawnItems.clearLayers(); window.plugin.drawTools.import(data); console.log('DRAWTOOLS: reset and imported drawn items');