Skip to content

Commit

Permalink
add escape to json string before send to server
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed May 24, 2016
1 parent 0e20aa4 commit eac4961
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/puppet-web-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,21 @@ class Bridge {
* Proxy Call to Wechaty in Bridge
*/
proxyWechaty(wechatyFunc, ...args) {
//const args = Array.prototype.slice.call(arguments, 1)
const argsJson = JSON.stringify(args)
function escape (key, val) {
if (typeof(val)!="string") return val;
return val
.replace(/[\\]/g, '\\\\')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t')
.replace(/[\"]/g, '\\"')
.replace(/\\'/g, "\\'")
} // http://stackoverflow.com/a/14137856/1123955
const argsJson = JSON.stringify(args, escape)

const wechatyScript = `return (Wechaty && Wechaty.${wechatyFunc}.apply(undefined, JSON.parse('${argsJson}')))`

log.silly('Bridge', 'proxyWechaty: ' + wechatyScript)
Expand Down

0 comments on commit eac4961

Please sign in to comment.