Skip to content

Instantly share code, notes, and snippets.

@uplus
Created June 4, 2024 11:05
Show Gist options
  • Save uplus/92ece3faf8c3f81a18946bd06fece330 to your computer and use it in GitHub Desktop.
Save uplus/92ece3faf8c3f81a18946bd06fece330 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const readline = require('readline');
const vm = require('vm');
// JSONPのコールバック関数名
const callbackName = process.argv[2];
if (!callbackName) {
console.error('Usage: jsonp-parser <callbackName>');
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
// stdinからJSONPデータを読み込む
let jsonpData = '';
rl.on('line', (line) => {
jsonpData += line;
});
rl.on('close', () => {
// JSONPのコールバック関数を定義
const context = {
// 適当に両方定義しておく
window: {
[callbackName]: (data) => {
console.log(JSON.stringify(data, null, 2));
},
},
[callbackName]: (data) => {
console.log(JSON.stringify(data, null, 2));
},
};
// JSONPスクリプトを実行
vm.createContext(context);
vm.runInContext(jsonpData, context);
});
@uplus
Copy link
Author

uplus commented Jun 11, 2024

This script extract JSON from JSONP response.

curl https://example.com/jsonp_endpoint | jsonp-parser "callbackFuncName"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment