-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript-params.js
54 lines (52 loc) · 1.81 KB
/
script-params.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*eslint-env browser*/
"use strict";
module.exports = getParams;
function getParams(scriptName) {
var i, j,
match,
script,
location,
attr,
name,
re = new RegExp("^(.*)" + scriptName + "(?:[\\?\\.]|$)", "i");
var params = {};
// Find the <script> that loads us, so we can divine our parameters
// from its attributes.
var scripts = document.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++) {
script = scripts[i];
if (scriptName && script.src && (match = script.src.match(re))) {
location = match[1];
}
if (location) {
if (script.dataset) {
for (name in script.dataset) {
if (script.dataset.hasOwnProperty(name)) {
params[name] = script.dataset[name];
}
}
} else if (script.attributes) {
var dataRe = /^data-(.*)$/,
letterAfterDash = /-([a-z])/g,
/*jshint -W083 */
upperCaseChar = function (_, c) {
return c.toUpperCase();
};
/*jshint +W083 */
for (j = 0; j < script.attributes.length; j++) {
attr = script.attributes[j];
match = attr.name.match(dataRe);
if (match) {
params[match[1].replace(letterAfterDash, upperCaseChar)] = attr.value;
}
}
}
// Permits multiple boot <scripts>; by removing as they are
// discovered, next one finds itself.
script.parentNode.removeChild(script);
params.location = location;
break;
}
}
return params;
}