Coppersmina

Enhances Coppermine galleries with direct links, color coded border and other tricks. See source code for more info and settings.

当前为 2014-08-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Coppersmina
  3. // @namespace github.com/ariacorrente/coppersmina
  4. // @description Enhances Coppermine galleries with direct links, color coded border and other tricks. See source code for more info and settings.
  5. // @version 0.6
  6. // @match http://*/*
  7. // @grant GM_registerMenuCommand
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // ==/UserScript==
  11.  
  12. /*
  13. Copyright (C) 2014 ariacorrente
  14.  
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation, either version 3 of the License, or
  18. (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program. If not, see <http://www.gnu.org/licenses/>.
  27.  
  28. Contact: https://github.com/ariacorrente/coppersmina
  29. */
  30.  
  31. /*
  32. # Coppersmina
  33.  
  34. User script that enhances Coppermine galleries with direct links, color coded
  35. border and other tiny features.
  36.  
  37. Allows the use of mass downloaders like DownThemAll! and FlashGot.
  38.  
  39. ## Features
  40.  
  41. - Replace links in thumbnails to point directly to the high defintion image
  42. - Add a colored border indicating the size of the image
  43. - Append image information into the thumbnail's caption and remove the tooltip
  44. - The "always run" feature allows the execution of the script even if the site
  45. is not automatically detected as a Coppermine gallery
  46.  
  47. ## Settings
  48.  
  49. At the top of the script there are some variables the user can change to
  50. customize the script behaviour.
  51.  
  52. - Choose what information to display into the capton of the thumbnail
  53. - Toggle the display of the colored border
  54. - Choose if the color of the border is calculated from the image KB size or the
  55. image dimensions
  56. - Choose the colors to use for the border and the rages of size for each color
  57. - Width of the colored border
  58. - Toggle the "always run" feature
  59. - Toggle the auto deactivation of the "always run" feature after 24 hours of the
  60. activation
  61. */
  62.  
  63. (function() {
  64. // START OF USER SETTINGS
  65. //
  66. // Image info to display under the thumbnail
  67. // All the available options are:
  68. // var imageInfo = ['Filename, 'Filesize', 'Dimensions', 'Date added'];
  69. // Empty array disable this feature
  70. var imageInfo = ['Filesize'];
  71. // If image info is under the image the tooltips are no more required
  72. var removeTooltips = true;
  73. // Add a colored border based on image size
  74. var colorBorder = true;
  75. // The border can be colored using two possible data source:
  76. // - "Dimensions": area of the image in pixels.
  77. // - "Filesize": KiloBytes of the HD image. File compession and image content may make this
  78. // option less reliable.
  79. var colorByWhat = 'Dimensions';
  80. // Colors to be used for the border.
  81. // WARNING: The sizes must be in increasing order to work
  82. // Size is expressed in KB
  83. // Colors are CSS colors so you can use names or numbers
  84. var colorCode = [{size: 0, color: 'lightgray'},
  85. {size: 250, color: 'lightgreen'},
  86. {size: 500, color: 'yellow'},
  87. {size: 1000, color: 'red'},
  88. {size: 2000, color: 'magenta'}];
  89. // Thumbnail border size in pixels (don't add unit of measure in borderSize)
  90. var borderSize = 3;
  91. // Enable the feature tu run this script even if the site is not detected as a Coppermine gallery
  92. var canRunAlways = true;
  93. // After 24 hours the "run always" feature will be disabled automatically
  94. var autoDisableRunAlways = true;
  95. //
  96. // END OF USER SETTINGS
  97.  
  98. var debugMode = false;
  99.  
  100. function clog(msg) {
  101. if(debugMode) {
  102. console.log(msg);
  103. }
  104. }
  105.  
  106. function enableRunAlways() {
  107. GM_setValue("runAlways", true);
  108. GM_setValue("timeRunAlwaysEnabled", Date.now() );
  109. }
  110.  
  111. function disableRunAlways() {
  112. GM_setValue("runAlways", false);
  113. }
  114.  
  115. function checkRunAlways() {
  116. //Load saved config from disk
  117. var isRunAlways = GM_getValue("runAlways");
  118. if(isRunAlways) {
  119. GM_registerMenuCommand("Stop running Coppersmina in every site", disableRunAlways, 'C');
  120. } else {
  121. GM_registerMenuCommand("Run Coppersmina in every site", enableRunAlways, 'C');
  122. }
  123.  
  124. if(autoDisableRunAlways) {
  125. //If runAlways is enabled by more than 24 hours disable automatically
  126. //because it's probably been forgotten enabled
  127. var timeEnabled = GM_getValue("timeRunAlwaysEnabled", Date.now());
  128. var timeNow = Date.now();
  129. //elapsed in ms
  130. var elapsed = timeNow - timeEnabled;
  131. elapsed = elapsed / (1000 * 60 * 60);
  132. if(elapsed > 24) {
  133. disableRunAlways();
  134. }
  135. }
  136.  
  137. return isRunAlways;
  138. }
  139.  
  140. function runCoppersmina() {
  141. //find all the anchors around the the thumbnails and iterate
  142. var anchors = document.querySelectorAll('a[href*=displayimage]');
  143. clog("Found " + anchors.length + " anchors");
  144. if(anchors.length === 0) { return; }
  145. for(var i = 0; i < anchors.length; i++) {
  146. var anchor = anchors[i];
  147. var thumbnail = anchor.querySelector('img');
  148. if(thumbnail === null) {
  149. clog("Thumbnail not found");
  150. continue;
  151. }
  152. //find the text field under the thumbnail
  153. var caption = anchor.parentNode.querySelector("span");
  154. if(caption === null) {
  155. clog("Caption not found");
  156. continue;
  157. }
  158. //add the old link to the caption
  159. var oldLink = document.createElement('a');
  160. oldLink.innerHTML = "Original link";
  161. oldLink.href = anchor.href;
  162. caption.appendChild(document.createElement('br'));
  163. caption.appendChild(oldLink);
  164. //replace the thumbnail link with a direct link to the HD image
  165. var hdUrl = thumbnail.src.replace(/thumb_/, "");
  166. anchors[i].href = hdUrl;
  167. //Copy image information from title attribute and append to caption
  168. var regex, found;
  169. for(var j = 0; j < imageInfo.length; j++) {
  170. regex = new RegExp(imageInfo[j] + '=(.*)');
  171. found = regex.exec(thumbnail.title);
  172. if(found !== null) {
  173. var extraInfo = document.createElement('span');
  174. extraInfo.innerHTML = found[1];
  175. caption.appendChild(document.createElement('br'));
  176. caption.appendChild(extraInfo);
  177. } else {
  178. clog('Image info "' + imageInfo[j] + '" not found');
  179. }
  180. }
  181.  
  182. if(colorBorder) {
  183. //Calculate image weight to chose a border color
  184. var imageWeight;
  185. regex = new RegExp(colorByWhat + '=(.*)');
  186. found = regex.exec(thumbnail.title);
  187. if(found) {
  188. if(colorByWhat == "Dimensions") {
  189. var sizes = found[1].split('x');
  190. var area = sizes[0] * sizes[1];
  191. //Divide to have comparable sizes with "FileSize" wich is expressed in KB
  192. imageWeight = area / 8192;
  193. } else {
  194. //Remove last 3 characters occupied by "KiB"
  195. imageWeight = found[1].slice(0, -3);
  196. }
  197. } else {
  198. clog('Image info "' + colorByWhat + '" not found, unable to color the border');
  199. }
  200.  
  201. //Add the colored border to the thumbnail
  202. var newColor;
  203. for(j = 0; j < colorCode.length; j++) {
  204. if(imageWeight > colorCode[j].size) {
  205. newColor = colorCode[j].color;
  206. }
  207. }
  208. thumbnail.style.border = borderSize + 'px solid ' + newColor;
  209. }
  210.  
  211. //Remove the tooltip if required
  212. if(removeTooltips) {
  213. thumbnail.title = "";
  214. }
  215. }
  216. }
  217.  
  218. var isRunAlways = false;
  219. if(canRunAlways) {
  220. isRunAlways = checkRunAlways();
  221. }
  222. // Detect if it's a coppermine gallery
  223. if(isRunAlways || document.querySelector('a[href*=coppermine]') !== null) {
  224. clog("Coppersmining");
  225. runCoppersmina();
  226. }
  227.  
  228. }());