Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip invalid characters from beginning and end of pasted strings #1242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion plugins/draw-tools.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down