Skip to content

Commit

Permalink
Escape JS string for URLs
Browse files Browse the repository at this point in the history
Thanks c't
  • Loading branch information
stefanw committed Jan 27, 2024
1 parent 7ecb1d0 commit 3505f3e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/tabrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Action, Actions } from './types.js'

import { STATUS_MESSAGE } from './const.js'
import converters from './converters.js'
import { makeTimeout } from './utils.js'
import { escapeJsString, makeTimeout } from './utils.js'

class TabRunner {
tabId: number
Expand Down Expand Up @@ -84,7 +84,7 @@ class TabRunner {
return [`document.querySelector('${action.click}').click()`]
}
} else if ('url' in action) {
return [`document.location.href = '${action.url}';`]
return [`document.location.href = '${escapeJsString(action.url)}';`]
} else if ('href' in action) {
return [`document.location.href = document.querySelector('${action.href}').href;`]
} else if ('captcha' in action) {
Expand Down
7 changes: 5 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function makeTimeout (ms: number) {
}
}

const escapeJsString = (str) => {
return str.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')
}

export {
interpolate,
makeTimeout
escapeJsString, interpolate, makeTimeout
}

0 comments on commit 3505f3e

Please sign in to comment.