Flickr Large Embed

Based on 'Flickr Original Link' (https://greasyfork.org/en/scripts/3135-flickr-original-link-beta). Adds an 'Embed' field containing HTML code for each photo.

当前为 2014-11-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Flickr Large Embed
  3. // @namespace https://greasyfork.org/en/scripts/6699-flickr-large-embed
  4. // @description Based on 'Flickr Original Link' (https://greasyfork.org/en/scripts/3135-flickr-original-link-beta). Adds an 'Embed' field containing HTML code for each photo.
  5. // @include /^https?:\/\/.*\.?flickr\.com\/photos\/.*/
  6. // @version 2014-11-28
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
  10. // ==/UserScript==
  11. //
  12.  
  13. var source = "";
  14.  
  15. /*
  16. * support function
  17. */
  18.  
  19. function addEmbedToNewPhotoPage()
  20. {
  21. var size = document.documentElement.innerHTML.match(/"sizes":{(.+?})}/i);
  22.  
  23. var mWidth, mHeight, mLink, mSize, length;
  24. mSize = size[0].match(/"width":"?\d+"?,"height":"?\d+"?,/ig);
  25. mLink = size[0].match(/"url":"[^"]+"/ig);
  26. length = mLink.length;
  27.  
  28. var embedSize = 0;
  29. var linkSize = 0;
  30. var embedHeight = 0;
  31. var embedWidth = 0;
  32. for (var k = 0; k < length; k++) {
  33.  
  34. var myArray = mSize[k].match(/:\w+,/g);
  35. var width = parseInt(myArray[0].replace(':','').replace(',',''));
  36. var height = parseInt(myArray[1].replace(':','').replace(',',''));
  37.  
  38. mSize[k] = mSize[k].replace(/"width":(\d+),"height":(\d+),/i, "$1 x $2");
  39. mLink[k] = "http:" + mLink[k].replace(/"url":"([^"]+)"/i, "$1").replace(/\\/g, "");
  40.  
  41. if (embedSize == 0 && (width >= 800 || height >= 600) ) {
  42. embedSize = k;
  43. embedHeight = height;
  44. embedWidth = width;
  45. }
  46. if (linkSize == 0 && (width >= 2048 || height >= 1024) ) {
  47. linkSize = k;
  48. }
  49. if (embedSize == 0 && k == length-1) {
  50. embedSize = k;
  51. embedHeight = height;
  52. embedWidth = width;
  53. }
  54. if (linkSize == 0 && k == length-1) {
  55. linkSize = k;
  56. }
  57. }
  58.  
  59. var maxWidth = '';
  60. if (embedHeight < embedWidth) {
  61. // Landscape image: full width and auto-height
  62. maxWidth= 'style="max-width: ' + embedWidth + 'px"';
  63. embedWidth = '100%';
  64. } else {
  65. embedWidth += 'px';
  66. }
  67. var embedCode = '<p><a href="' + mLink[linkSize] + '" target="_blank" border="0"><img src="' + mLink[embedSize] + '" width="' + embedWidth + '" height="auto" border="0" ' + maxWidth + ' /></a></p>';
  68.  
  69. var insertLocation = $('.sub-photo-right-row1');
  70. if (insertLocation.length > 0) {
  71. insertLocation.append('<div style="color: black; display:block;">Embed <input type="text" name="textfield" onclick="this.select();" style="width:350px;" id="EMBED" value="Loading"/></div>');
  72. $('#EMBED').val(embedCode);
  73. }
  74. }
  75.  
  76. function addEmbedToNavPhotoPage(sizes)
  77. {
  78. var html = $.parseHTML(sizes);
  79.  
  80. var embedUrl = false;
  81. var linkUrl = false;
  82. var currentEmbedWidth = 100000;
  83. var currentEmbedHeight = 100000;
  84. var currentLinkWidth = 100000;
  85. var currentLinkHeight = 100000;
  86. var maxWidth = 0;
  87. var maxHeight = 0;
  88. var maxUrl = false;
  89. $.each(html, function( index, size ) {
  90. if (size.nodeName == 'LI' && size.textContent != "View all sizes") {
  91. var link = size.childNodes[1].href;
  92. var myArray = size.textContent.match(/\w+/g);
  93. var width = parseInt(myArray[1]);
  94. var height = parseInt(myArray[2]);
  95. if ((width >= 800 || height >= 600) && width < currentEmbedWidth && height < currentEmbedHeight) {
  96. embedUrl = link;
  97. currentEmbedHeight = height;
  98. currentEmbedWidth = width;
  99. }
  100. if ((width >= 2048 || height >= 1024) && width < currentLinkWidth && height < currentLinkHeight ) {
  101. linkUrl = link;
  102. currentLinkHeight = height;
  103. currentLinkWidth = width;
  104. }
  105. if (width > maxWidth && height > maxHeight) {
  106. maxWidth = width;
  107. maxHeight = height;
  108. maxUrl = link;
  109. }
  110. }
  111. });
  112.  
  113. if (!linkUrl) {
  114. linkUrl = maxUrl;
  115. currentLinkHeight = maxHeight;
  116. currentLinkWidth = maxWidth;
  117. }
  118. if (!embedUrl) {
  119. embedUrl = maxUrl;
  120. currentEmbedHeight = maxHeight;
  121. currentEmbedWidth = maxWidth;
  122. }
  123.  
  124. if (linkUrl && embedUrl) {
  125. var maxWidth= '';
  126. if (currentEmbedHeight < currentEmbedWidth) {
  127. // Landscape image: full width and auto-height
  128. maxWidth= 'style="max-width: ' + currentEmbedWidth + 'px"';
  129. currentEmbedWidth = '100%';
  130. } else {
  131. currentEmbedWidth += 'px';
  132. }
  133.  
  134. var embedCode = '<p><a href="' + linkUrl + '" target="_blank" border="0"><img src="' + embedUrl + '" width="' + currentEmbedWidth + '" height="auto" border="0" ' + maxWidth + ' /></a></p>';
  135. var insertLocation = $('.sub-photo-right-row1');
  136. if (insertLocation.length > 0) {
  137. insertLocation.append('<div style="color: black; display:block;">Embed <input type="text" name="textfield" onclick="this.select();" style="width:350px;" id="EMBED" value="Loading"/></div>');
  138. $('#EMBED').val(embedCode);
  139. }
  140. }
  141. }
  142. function action_singlephoto()
  143. {
  144. addEmbedToNewPhotoPage();
  145. var target = $('body')[0];
  146. var config = {
  147. childList : true,
  148. subtree : true,
  149. };
  150. var observer = new MutationObserver(function(mutations, ob) {
  151. //console.log("Mutation occurred");
  152. if ($('#EMBED').length > 0) {
  153. //console.log("Embed input exists");
  154. return false;
  155. } else {
  156. // Moved to the previous/next page : load the sizes and add the Embed code
  157. var sizes = $('.sizes')[0].innerHTML;
  158. addEmbedToNavPhotoPage(sizes);
  159. }
  160. });
  161. observer.observe(target, config);
  162. }
  163.  
  164. function action_photostream()
  165. {
  166. source = document.documentElement.innerHTML.match(/"sizes":.*?}}/g);
  167.  
  168. if (source == null) {
  169. console.log('err: no source');
  170. return false;
  171. }
  172. var t1 = $('a[data-track="prev"]').attr('href');
  173. if (t1 != null) {
  174. var t2 = $('span.this-page').text();
  175. t1 = t1.replace(/\/page\d+\//i, "/page" + t2 + "/");
  176. var $div = $('<div>');
  177. $div.load(t1, function() {
  178. var source2 = $(this).text().match(/"sizes":.*?}}/g);
  179.  
  180. source = source.concat(source2);
  181. });
  182. }
  183. var target = $('body')[0];
  184. var config = {
  185. childList : true,
  186. subtree : true,
  187. };
  188. var observer = new MutationObserver(function(mutations, ob) {
  189. //console.log("Mutation occurred");
  190. if ($('.myMotherFuckingLink').filter(':first').length > 0) {
  191. //console.log("Download link existed");
  192. return false;
  193. }
  194. var photoDisplayItem = $('.photo-display-item');
  195. //console.time('Insert links');
  196. $('.myMotherFuckingLink').remove();
  197. photoDisplayItem.each(showLink);
  198. //console.timeEnd('Insert links');
  199. });
  200. observer.observe(target, config);
  201. }
  202.  
  203. function pageType()
  204. {
  205. var title = $('head title').text();
  206. var type = "none";
  207. if (title.match(/flickr.+photostream/i) != null) type = 'photostream';
  208. else if (title.match(/an album on flickr/i) != null) type = 'photostream';
  209. else if (title.match(/flickr - photo sharing/i) != null) type = 'singlephoto';
  210. else if (title.match(/favorite photos and videos/i) != null) type = 'favorite';
  211. else if (title.match(/from the people you follow/i)!=null) type = 'favorite';
  212. // else if (document.URL.match(/flickr\.com\/groups\//i) != null) type = 'favorite';
  213. else if (title.match(/flickr.+pool$/i) != null) type = 'favorite';
  214. //console.log("page type " + type);
  215.  
  216. return type;
  217. }
  218.  
  219. function showLink(index, elem)
  220. {
  221. var $e = $(elem);
  222. var photoId = $e.attr('data-photo-id');
  223. $e.find('.attribution-block').append('<div class="myMotherFuckingLink" style="color: white; display: block; opacity: 0.5">Embed <input type="text" name="textfield" onclick="this.select();" style="width:100px;" id="EMBED_' + photoId + '" value="Loading"/></div>');
  224.  
  225. var embedUrl = false;
  226. var linkUrl = false;
  227. var currentEmbedWidth = 100000;
  228. var currentEmbedHeight = 100000;
  229. var currentLinkWidth = 100000;
  230. var currentLinkHeight = 100000;
  231. var maxWidth = 0;
  232. var maxHeight = 0;
  233. var maxUrl = false;
  234.  
  235. for (var i = 0; i < source.length; i++) {
  236.  
  237. if (source[i].indexOf(photoId) == 40) {
  238.  
  239. var sizes = JSON.parse('{'+source[i]+'}');
  240. $.each(sizes.sizes, function( index, size ) {
  241. var width = parseInt(size.width);
  242. var height = parseInt(size.height);
  243.  
  244. if ((width >= 800 || height >= 600) && width < currentEmbedWidth && height < currentEmbedHeight) {
  245. embedUrl = size.url;
  246. currentEmbedHeight = height;
  247. currentEmbedWidth = width;
  248. }
  249. if ((width >= 2048 || height >= 1024) && width < currentLinkWidth && height < currentLinkHeight ) {
  250. linkUrl = size.url;
  251. currentLinkHeight = height;
  252. currentLinkWidth = width;
  253. }
  254. if (width > maxWidth && height > maxHeight) {
  255. maxWidth = width;
  256. maxHeight = height;
  257. maxUrl = size.url;
  258. }
  259.  
  260. });
  261.  
  262. if (!linkUrl) {
  263. linkUrl = maxUrl;
  264. currentLinkHeight = maxHeight;
  265. currentLinkWidth = maxWidth;
  266. }
  267. if (!embedUrl) {
  268. embedUrl = maxUrl;
  269. currentEmbedHeight = maxHeight;
  270. currentEmbedWidth = maxWidth;
  271. }
  272.  
  273. if (linkUrl && embedUrl) {
  274. var maxWidth= '';
  275. if (currentEmbedHeight < currentEmbedWidth) {
  276. // Landscape image: full width and auto-height
  277. maxWidth= 'style="max-width: ' + currentEmbedWidth + 'px"';
  278. currentEmbedWidth = '100%';
  279. } else {
  280. currentEmbedWidth += 'px';
  281. }
  282. var embedCode = '<p><a href="' + linkUrl + '" target="_blank" border="0"><img src="' + embedUrl + '" width="' + currentEmbedWidth + '" height="auto" border="0" ' + maxWidth + ' /></a></p>';
  283. $('#EMBED_'+photoId).val(embedCode);
  284. }
  285. }
  286. }
  287. }
  288.  
  289. /*
  290. * end support
  291. */
  292. var type = pageType();
  293.  
  294. if (type == 'photostream') {
  295. action_photostream();
  296. } else if (type == 'singlephoto') {
  297. action_singlephoto();
  298. }