CSS domain injector tool


/ Published in: JavaScript
Save to your folder(s)

This handy tool takes raw CSS and finds all relative URLs for background images and injects the absolute path. After which all the background images are displayed on the page. This is useful if you need a quick way to save all background images at once.


Copy this code and paste it in your HTML
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>CSS Domain Injector Tool</title>
  5. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  6. <style>
  7. #output img { display: block; }
  8. html,
  9. body { font-family: sans-serif; }
  10. h4 { color: #999; }
  11. .textareas > div { margin: 0 0 0 2em; float: left; width: 48%; height: 25em; }
  12. .textareas label { display: block; }
  13. .textareas textarea { width: 100%; height: 90%; }
  14. .textareas > div:first-child { margin-left: 0; }
  15. #output, .textareas { overflow: hidden; }
  16. #path { width: 25%; }
  17. .warning { color: #c00; }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>CSS domain injector tool</h1>
  22. <h4>Want to grab all the background images from a CSS file? Enter a root domain and press "Inject domain and show all images" to output new CSS that converts all root-relative URLs to absolute URLs. Then, just "Save Page As" to save the image dump.</h4>
  23. <p>This tool ignores all relative paths, but outputs a message that it found one, so you'll have to manually convert those</p>
  24. <input id="path" value="" placeholder="Enter root domain here (e.g. //example.com)" />
  25. <button id="show-bg">Inject domain and show all images</button>
  26. <div class="textareas">
  27. <div>
  28. <textarea id="css-input" placeholder="Paste in your CSS code here"></textarea>
  29. <!--
  30. Example data for debugging
  31. <textarea id="css-input">
  32. background: url(/images/asdf.png);
  33. background: url( /images/asdf.png );
  34. background: url("/images/asdf.png");
  35. background: url( "/images/asdf.png" );
  36. background: url('/images/asdf.png');
  37. background: url( '/images/asdf.png' );
  38. background: url(/images/asdf.png);
  39. background: url( /images/asdf.png );
  40. background: url("/images/asdf.png");
  41. background: url( "/images/asdf.png" );
  42. background: url('/images/asdf.png');
  43. background: url( '/images/asdf.png' );
  44. background: url(//something.com/images/asdf.png);
  45. background: url( //something.com/images/asdf.png );
  46. background: url("//something.com/images/asdf.png");
  47. background: url( "//something.com/images/asdf.png" );
  48. background: url('//something.com/images/asdf.png');
  49. background: url( '//something.com/images/asdf.png' );
  50. background: url(http://something.com/images/asdf.png);
  51. background: url( http://something.com/images/asdf.png );
  52. background: url("http://something.com/images/asdf.png");
  53. background: url( "http://something.com/images/asdf.png" );
  54. background: url('http://something.com/images/asdf.png');
  55. background: url( 'http://something.com/images/asdf.png' );
  56.  
  57. background: url(../this/is/relative.png);
  58. background: url(this/is/also/relative.png);
  59.  
  60. </textarea>-->
  61. </div>
  62. <div>
  63. <textarea id="css-output" placeholder="New CSS with the added domain will be here"></textarea>
  64. </div>
  65. </div>
  66. <div style="overflow: hidden;">
  67. <h4><span id="total-img">0</span> Image/s shown<br />
  68. <span id="total-relative" class="warning">0</span> relative path/s:</h4>
  69. <div id="output"></div>
  70. </div>
  71. <script>
  72. var changePaths = function () {
  73. var totalImg = 0;
  74. var totalRelative = 0;
  75. var cssInput = $('#css-input').val();
  76. var pattern = /url\(["']?(.*?)["']?\)/g;
  77. var reg = cssInput.match(/url\(["']?(.*?)["']?\)/gi);
  78. var cssOutput = $('#css-input').val();
  79. var path = $('#path').val();
  80. var imgOutput = '';
  81. var format = function (str) {
  82. // strip out all extra white space and quotes within url(...)
  83. str = str.replace(/\(\s*/gi, '(');
  84. str = str.replace(/\(["|']?/gi, '(');
  85. str = str.replace(/\s*\)/gi, ')');
  86. str = str.replace(/['|"]?\)/gi, ')');
  87. return str;
  88. };
  89. var extractPath = function (str) {
  90. // returns just the path of the url (everything inside the parens)
  91. return str.replace(/[url\(|\)]/gi, '');
  92. };
  93. var insertNewPath = function (str) {
  94. return str.replace(/url\(/gi, 'url(' + path);
  95. };
  96.  
  97. $.each(reg, function (i) {
  98. var url = format(reg[i]);
  99. var filename = '';
  100. var isDomainAbsolute = url.indexOf('//') === 4 || url.indexOf('http') === 4;
  101. var isRootRelative = !isDomainAbsolute && url.indexOf('/') === 4;
  102.  
  103. filename = url.substring(url.lastIndexOf('/')).replace(/\)/gi, '');
  104. cssOutput = format(cssOutput);
  105. if (!isDomainAbsolute && !isRootRelative) {
  106. // we are dealing with a relative link here - not accounting for that in output, so flag it instead
  107. totalRelative += 1;
  108. imgOutput += '<p><span class="warning">Relative path found for filename, <em>' + filename + '</em></span></p>';
  109. } else if (isRootRelative && !isDomainAbsolute) {
  110. cssOutput = cssOutput.replace(url, 'url(' + path + url.substring(4, url.length - 1) + ')');
  111. imgOutput += '<img data-elseif src="' + path + url.substring(4, url.length - 1) + '" />';
  112. } else {
  113. imgOutput += '<img data-else src="' + extractPath(url) + '" />';
  114. }
  115. });
  116. $('#total-img').html(reg.length);
  117. $('#total-relative').addClass('warning').html(totalRelative);
  118. $('#css-output').val(cssOutput);
  119. $('#output').html(imgOutput);
  120. };
  121. $('#show-bg').on('click', function () {
  122. changePaths();
  123. });
  124. </script>
  125. </body>
  126. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.