Skip to content

Commit

Permalink
(fix) replace for all parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kameshsampath committed Aug 31, 2020
1 parent 2af113c commit 36c0559
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules/
/public/
yarn.lock
.tool-versions
2 changes: 1 addition & 1 deletion preview-src/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Hello %USER% your password is %PASSWORD%
You need to https://console.openshift.com[login,role=userfied-link]


Hello $USERNAME your password is $PASSWORD
I like %COLOR% color

== Cu solet

Expand Down
34 changes: 21 additions & 13 deletions src/js/07-userparams-behaviour.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
document.addEventListener('DOMContentLoaded', function () {
var queryString = window.location.search

function getParameterByName (name, url) {
/*
* Thanks to https://gomakethings.com/getting-all-query-string-values-from-a-url-with-vanilla-js/
*/
function getParams (url) {
if (!url) url = window.location.href
name = name.replace(/[[\]]/g, '\\$&')
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
var results = regex.exec(url)
if (!results) return null
if (!results[2]) return ''
var val = decodeURIComponent(results[2].replace(/\+/g, ' '))
console.log(name + '=' + val)
return val
var params = {}
var parser = document.createElement('a')
parser.href = url
var query = parser.search.substring(1)
var vars = query.split('&')
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=')
params[pair[0]] = decodeURIComponent(pair[1])

This comment has been minimized.

Copy link
@mulbc

mulbc Sep 22, 2020

Member

why are you not URL decoding the variable's name?

}
return params
}

function walkText (node, pattern, value) {
function replaceParamsInNodes (node, pattern, value) {
if (node.nodeType === 3) {

This comment has been minimized.

Copy link
@mulbc

mulbc Sep 22, 2020

Member

Could you use Node.TEXT_NODE instead of 3? That way the code is more obvious what it does...

var re = new RegExp(pattern, 'g')
var text = node.data
Expand All @@ -25,13 +30,16 @@ document.addEventListener('DOMContentLoaded', function () {
}
if (node.nodeType === 1 && node.nodeName !== 'SCRIPT') {
for (var i = 0; i < node.childNodes.length; i++) {
walkText(node.childNodes[i], pattern, value)
replaceParamsInNodes(node.childNodes[i], pattern, value)
}
}
}

walkText(document.body, '(%USER%|\\$USERNAME)', getParameterByName('USER'))
walkText(document.body, '(%PASSWORD%|\\$PASSWORD)', getParameterByName('PASSWORD'))
var allParams = getParams()
var keys = Object.keys(allParams)
for (var i = 0; i < keys.length; i++) {
replaceParamsInNodes(document.body, '(%' + keys[i].toUpperCase() + '%)', allParams[keys[i]])
}

document.querySelectorAll('.userfied-link').forEach(function (el) {
el.href += queryString
Expand Down

0 comments on commit 36c0559

Please sign in to comment.