Created
March 28, 2016 09:53
-
-
Save silicakes/4614c46a59aee541686b to your computer and use it in GitHub Desktop.
get all interna, external and data URLs present inside a stylesheet
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
/** | |
* @description Returns all image/font/whatever URLs from all stylesheets present in the document | |
* @param styles {object|StyleSheetList} https://developer.mozilla.org/en-US/docs/Web/API/StyleSheetList | |
* @returns {array|string} | |
*/ | |
function getURLsFromCSS(styles) { | |
var urls = []; | |
for (var x in styles) { | |
var style = styles[x]; | |
var rules = style.rules; | |
for (var y in rules) { | |
var rule = rules[y]; | |
var text = rule.cssText; | |
var match = text && text.match(/url\((.*?)\)/) | |
if (!!match) { | |
var url = match[1].replace(/('|")/g, ''); | |
urls.push(url); | |
} | |
} | |
} | |
return urls; | |
} | |
//demo | |
var styles = window.document.styleSheets; | |
getURLsFromCSS(styles); // ["/images/spinners/octocat-spinner-64.gif","/images/modules/jobs/logo.png","http://example.com/images/modules/pulls/progressive-disclosure-line@2x.png", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAnsC7BpEAAAAABJRU5ErkJggg=="] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment