Created
June 4, 2024 11:05
-
-
Save uplus/92ece3faf8c3f81a18946bd06fece330 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script extract JSON from JSONP response.