From 6bdc36c32a5541df269bb73149c83835c80cad91 Mon Sep 17 00:00:00 2001 From: retsoksirhc Date: Fri, 29 Sep 2017 23:34:44 -0500 Subject: [PATCH 1/2] strip garbage from pasted text block --- plugins/draw-tools.user.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index 25a7101ff..30d3b46a3 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'); From 3ce55f8a971ec200b21c0fe08b238becb8b47757 Mon Sep 17 00:00:00 2001 From: retsoksirhc Date: Fri, 29 Sep 2017 23:43:44 -0500 Subject: [PATCH 2/2] whitespace :( --- plugins/draw-tools.user.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index 30d3b46a3..4425348ca 100644 --- a/plugins/draw-tools.user.js +++ b/plugins/draw-tools.user.js @@ -476,19 +476,19 @@ window.plugin.drawTools.optPaste = function() { } else { - 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 + 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);