🏠 Home 

InterfaceLIFT | Download Largest Wallpaper Variant

Adds a button to instantly download the optimum (largest) wallpaper image variant base on its total megapixels

  1. // ==UserScript==
  2. // @name InterfaceLIFT | Download Largest Wallpaper Variant
  3. // @namespace de.sidneys.userscripts
  4. // @homepage https://gist.githubusercontent.com/sidneys/32af71f8a3260cee2baef026837151a5/raw/
  5. // @version 11.11.11
  6. // @description Adds a button to instantly download the optimum (largest) wallpaper image variant base on its total megapixels
  7. // @author sidneys
  8. // @icon https://interfacelift.com/favicon.ico
  9. // @include http*://interfacelift.com/wallpaper/*
  10. // @require https://greasyfork.org/scripts/38888-greasemonkey-color-log/code/Greasemonkey%20%7C%20Color%20Log.js
  11. // @require https://greasyfork.org/scripts/38889-greasemonkey-waitforkeyelements-2018/code/Greasemonkey%20%7C%20waitForKeyElements%202018.js
  12. // @run-at document-end
  13. // ==/UserScript==
  14. /**
  15. * @default
  16. * @constant
  17. */
  18. const DEBUG = false;
  19. /**
  20. * @default
  21. * @constant
  22. */
  23. const defaultBaseUrl = 'https://InterfaceLIFT.com/wallpaper/7yz4ma1/';
  24. /**
  25. * Convert image dimensions to megapixels
  26. * @param {String} dimensions - Image dimensions (e.g. '1920x1080')
  27. * @returns {Number} Image megapixels (e.g. 2.07)
  28. */
  29. let convertDimensionsToMegapixels = (dimensions) => {
  30. console.debug('convertDimensionsToMegapixels');
  31. let width = dimensions.match(/\d+/g)[0];
  32. let height = dimensions.match(/\d+/g)[1];
  33. return parseFloat(((width * height) / 1048576).toFixed(2));
  34. };
  35. /**
  36. * Get wallpaper id
  37. * @param {String} url - InterfaceLIFT wallpaper url
  38. * @returns {String} InterfaceLIFT wallpaper id
  39. */
  40. let getWallpaperId = (url) => {
  41. console.debug('getWallpaperId');
  42. const wallpaperId = url.match(/\d+/)[0];
  43. return wallpaperId.length < 5 ? `0${wallpaperId}` : `${wallpaperId}`;
  44. };
  45. /**
  46. * Get wallpaper name
  47. * @param {String} url - InterfaceLIFT wallpaper url
  48. * @returns {String} InterfaceLIFT wallpaper name
  49. */
  50. let getWallpaperName = (url) => {
  51. console.debug('getWallpaperName');
  52. return url.replace(/.*\/|\.[^.]*$/g, '');
  53. };
  54. /**
  55. * Get wallpaper url
  56. * @param {String} id - InterfaceLIFT wallpaper id
  57. * @param {String} name - InterfaceLIFT wallpaper name
  58. * @param {String} dimensions - Image dimensions (e.g. '1920x1080')
  59. * @returns {String} InterfaceLIFT wallpaper name
  60. */
  61. let getWallpaperUrl = (id, name, dimensions) => {
  62. console.debug('getWallpaperName');
  63. const wallpaperNameClean = name.replace(/_/g, '');
  64. return `${defaultBaseUrl}${id}_${wallpaperNameClean}_${dimensions}.jpg`;
  65. };
  66. /**
  67. * Parse optimum image dimensions from InterfaceLIFT selector
  68. * @param {HTMLElement} selectElement - InterfaceLIFT resolution selector dropdown
  69. * @returns {String} Image dimensions (e.g. '1920x1080')
  70. */
  71. let parseOptimumDimensions = (selectElement) => {
  72. console.debug('parseOptimumDimensions');
  73. const dimensionsList = [];
  74. const elementOptionList = selectElement.querySelectorAll('optgroup');
  75. elementOptionList.forEach((elem) => {
  76. dimensionsList.push(elem.querySelector('option').value);
  77. dimensionsList.sort((a, b) => {
  78. const megapixelsA = convertDimensionsToMegapixels(a);
  79. const megapixelsB = convertDimensionsToMegapixels(b);
  80. if (megapixelsA > megapixelsB) { return -1; }
  81. if (megapixelsA < megapixelsB) { return 1; }
  82. return 0;
  83. });
  84. });
  85. return dimensionsList[0];
  86. };
  87. /**
  88. * Adds largest resolution download controls
  89. * @param {Element} containerElement - Wallpaper container element
  90. */
  91. let addDownloadControls = (containerElement) => {
  92. console.debug('addDownloadControls');
  93. // Get wallpaper previews
  94. let itemList = containerElement.querySelectorAll('.item .preview');
  95. // Extend wallpaper previews
  96. itemList.forEach((element) => {
  97. // Get id, name
  98. const defaultWallpaperUrl = element.querySelector('a').getAttribute('href');
  99. const wallpaperId = getWallpaperId(defaultWallpaperUrl);
  100. const wallpaperName = getWallpaperName(defaultWallpaperUrl);
  101. // Get url
  102. const selectElement = element.querySelector('select');
  103. // Get optimal wallpaper data
  104. const optimumDimensions = parseOptimumDimensions(selectElement);
  105. const optimumMegapixels = convertDimensionsToMegapixels(optimumDimensions);
  106. const optimumUrl = getWallpaperUrl(wallpaperId, wallpaperName, optimumDimensions);
  107. // Form: Select optimal image
  108. selectElement.value = optimumDimensions;
  109. // Button: Add url to optimal image
  110. const buttonElement = document.createElement('div');
  111. element.appendChild(buttonElement);
  112. buttonElement.style.height = '28px';
  113. buttonElement.style.marginLeft = '44px';
  114. buttonElement.style.backgroundColor = 'rgba(150, 50, 50, 0.05)';
  115. buttonElement.classList.add('download');
  116. buttonElement.innerHTML = `
  117. <span style="display: inline-block; margin: 4px 8px; width: 145px; max-width: 145px; text-align: left; font-family: Arial, sans-serif; font-size: 11px; white-space: nowrap;">
  118. <b>${optimumDimensions}</b> (${optimumMegapixels} MP)
  119. </span>
  120. <div id="download_4113" style="display: inline-block; float: right;">
  121. <a download href="${optimumUrl}">
  122. <img src="/img_NEW/button_download.png" style="filter: hue-rotate(169deg) saturate(400%);">
  123. </a>
  124. </div>
  125. `;
  126. // Status
  127. console.info(wallpaperName, optimumUrl, optimumDimensions);
  128. });
  129. };
  130. /**
  131. * Init
  132. */
  133. let init = () => {
  134. console.info('init');
  135. waitForKeyElements('#page > div:nth-child(5)', (containerElement) => {
  136. addDownloadControls(containerElement);
  137. });
  138. };
  139. /**
  140. * @listens window:Event#load
  141. */
  142. window.addEventListener('load', () => {
  143. console.debug('window#load');
  144. init();
  145. });