Last active
May 25, 2018 16:31
-
-
Save KaKi87/b98dc1bd9d944165573a78736dc03a69 to your computer and use it in GitHub Desktop.
Get custom JS variables of a web page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
First version | |
*/ | |
function getCustomVariables(){ | |
let page_variables = Object.keys(window); // current page's variables | |
let default_variables = Object.keys(document.head.appendChild(document.createElement('iframe')).contentWindow); // empty DOM's variables | |
let custom_variables = page_variables.filter(variable => default_variables.indexOf(variable) == -1); // comparison | |
return custom_variables; | |
} | |
getCustomVariables().forEach(variable => console.log(variable, window[variable])); | |
/* | |
Second version | |
Sorted result | |
*/ | |
function getCustomVariables(){ | |
let page_variables = Object.keys(window); // current page's variables | |
let default_variables = Object.keys(document.head.appendChild(document.createElement('iframe')).contentWindow); // empty DOM's variables | |
let custom_variables = page_variables.filter(variable => default_variables.indexOf(variable) == -1); // comparison | |
let sorted_result = {}; | |
custom_variables.forEach(v => { | |
if(sorted_result[typeof(window.v)] === undefined) | |
sorted_result[typeof(window.v)] = []; | |
sorted_result[typeof(window.v)].push(v); | |
}); | |
return sorted_result; | |
} | |
console.log(getCustomVariables()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment