-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathgetScriptData.js
More file actions
31 lines (30 loc) · 960 Bytes
/
Copy pathgetScriptData.js
File metadata and controls
31 lines (30 loc) · 960 Bytes
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
/**
* Helper function to pull json data off of script tags.
*
* @param {string} name - The name of the data attribute to look up.
* Note that this must be unique on the page.
* @returns {Object} the decoded json value of the data attribute.
*
* Example: given you have the following html:
*
* <script src="myscript.js" data-someServerData='{"userId": "foobar"}'></script>
*
* you could use this function like so:
*
* const userId = getScriptData('someServerData').userId;
* console.log("the server that rendered this page claims the user is", userid);
*
*/
export default function getScriptData(name) {
name = name.toLowerCase();
const script = document.querySelector(`script[data-${name}]`);
try {
return JSON.parse(script.dataset[name]);
} catch (e) {
console.error('Failed to parse script data for script', name);
throw e;
}
}
export function hasScriptData(name) {
return !!document.querySelector(name);
}