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

added ability to remove a JSON prefix if sent #22

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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: 13 additions & 2 deletions WebContent/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function displayUI(theme, html) {

function extractData(rawText) {
var tokens, text = rawText.trim();

function test(text) {
return ((text.charAt(0) == "[" && text.charAt(text.length - 1) == "]") || (text.charAt(0) == "{" && text.charAt(text.length - 1) == "}"));
}
Expand Down Expand Up @@ -274,11 +273,23 @@ function init(data) {
});
}

function stripJsonPrefix(text) {
// Some implementations return a JSON_PREFIX to help avoid
// allowing your JSON replies to be turned into JSONP replies.
var JSON_PREFIXES = [")]}', ", ")]}',\n"];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other prefixes commonly used, like "for(;;);" and "while(1);" (this one by Google).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's great! that's why I made it an array for people to add more to. I hadn't heard of the ones you posted.
The ones I included were from AngularJS's notes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a box on the options for the extensions to enter custom prefixes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this pull request were accepted so I don't need to run my own version of JSONview... :-/

until that happens or anyone takes any interest in this pull request I'm not likely to dedicate any extra effort to it.

for (var i = 0 ; i< JSON_PREFIXES.length; i++) {
if (text.substr(0, JSON_PREFIXES[i].length) == JSON_PREFIXES[i]) {
text = text.substr(JSON_PREFIXES[i].length);
}
}
return text;
}

function load() {
var child, data;
if (document.body && (document.body.childNodes[0] && document.body.childNodes[0].tagName == "PRE" || document.body.children.length == 0)) {
child = document.body.children.length ? document.body.childNodes[0] : document.body;
data = extractData(child.innerText);
data = extractData(stripJsonPrefix(child.innerText));
if (data)
init(data);
}
Expand Down