Skip to content

Instantly share code, notes, and snippets.

@KaKi87
Last active May 25, 2018 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaKi87/b98dc1bd9d944165573a78736dc03a69 to your computer and use it in GitHub Desktop.
Save KaKi87/b98dc1bd9d944165573a78736dc03a69 to your computer and use it in GitHub Desktop.
Get custom JS variables of a web page
/*
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