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-27
  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. // TODO:
  14. // improve onclick
  15. // figure out how to refresh when navigating to previous/next on the photo page
  16. // style embed (opacity?)
  17.  
  18. var source = "";
  19.  
  20. /*
  21. * support function
  22. */
  23.  
  24. function action_singlephoto()
  25. {
  26. var size = document.documentElement.innerHTML.match(/"sizes":{(.+?})}/i);
  27.  
  28. var mWidth, mHeight, mLink, mSize, length;
  29. var strCss = ".bigButton {display : inline-block; cursor : pointer; border-style : solid; border-width : 2px; border-radius : 50px; padding : 15px 15px; font-size : 10pt; font-weight : bold;} .smallButton { display: inline-block; padding: 0.6em; margin: 0.4em; background-color: pink; border-radius:1.5em;font-size:10pt}";
  30. $('head').append('<style>' + strCss + '</style>');
  31. mSize = size[0].match(/"width":"?\d+"?,"height":"?\d+"?,/ig);
  32. mLink = size[0].match(/"url":"[^"]+"/ig);
  33. length = mLink.length;
  34.  
  35. var embedSize = 0;
  36. var linkSize = 0;
  37. for (var k = 0; k < length; k++) {
  38.  
  39. var myArray = mSize[k].match(/:\w+,/g);
  40. var width = parseInt(myArray[0].replace(':','').replace(',',''));
  41. var height = parseInt(myArray[1].replace(':','').replace(',',''));
  42.  
  43. mSize[k] = mSize[k].replace(/"width":(\d+),"height":(\d+),/i, "$1 x $2");
  44. mLink[k] = "http:" + mLink[k].replace(/"url":"([^"]+)"/i, "$1").replace(/\\/g, "");
  45.  
  46. if (embedSize == 0 && (width >= 800 || height >= 600) ) {
  47. embedSize = k;
  48. }
  49. if (linkSize == 0 && (width >= 2048 || height >= 1024) ) {
  50. linkSize = k;
  51. }
  52. if (embedSize == 0 && k == length-1) {
  53. embedSize = k;
  54. }
  55. if (linkSize == 0 && k == length-1) {
  56. linkSize = k;
  57. }
  58. }
  59.  
  60. var embedCode = '<a href="' + mLink[linkSize] + '" target="_blank" border="0"><img src="' + mLink[embedSize] + '" border="0"/></a>';
  61.  
  62. var insertLocation = $('.sub-photo-right-row1');
  63. if (insertLocation.length > 0) {
  64. 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>');
  65. $('#EMBED').val(embedCode);
  66. }
  67. }
  68.  
  69. function action_photostream()
  70. {
  71. source = document.documentElement.innerHTML.match(/"sizes":.*?}}/g);
  72.  
  73. if (source == null) {
  74. console.log('err: no source');
  75. return false;
  76. }
  77. var t1 = $('a[data-track="prev"]').attr('href');
  78. if (t1 != null) {
  79. var t2 = $('span.this-page').text();
  80. t1 = t1.replace(/\/page\d+\//i, "/page" + t2 + "/");
  81. var $div = $('<div>');
  82. $div.load(t1, function() {
  83. var source2 = $(this).text().match(/"sizes":.*?}}/g);
  84.  
  85. source = source.concat(source2);
  86. });
  87. }
  88. var target = $('body')[0];
  89. var config = {
  90. childList : true,
  91. subtree : true,
  92. };
  93. var observer = new MutationObserver(function(mutations, ob) {
  94. console.log("Mutation occurred");
  95. if ($('.myMotherFuckingLink').filter(':first').length > 0) {
  96. console.log("Download link existed");
  97. return false;
  98. }
  99. var photoDisplayItem = $('.photo-display-item');
  100. console.time('Insert links');
  101. $('.myMotherFuckingLink').remove();
  102. photoDisplayItem.each(showLink);
  103. console.timeEnd('Insert links');
  104. });
  105. observer.observe(target, config);
  106. }
  107.  
  108. function pageType()
  109. {
  110. var title = $('head title').text();
  111. var type = "none";
  112. if (title.match(/flickr.+photostream/i) != null) type = 'photostream';
  113. else if (title.match(/an album on flickr/i) != null) type = 'photostream';
  114. else if (title.match(/flickr - photo sharing/i) != null) type = 'singlephoto';
  115. else if (title.match(/favorite photos and videos/i) != null) type = 'favorite';
  116. else if (title.match(/from the people you follow/i)!=null) type = 'favorite';
  117. // else if (document.URL.match(/flickr\.com\/groups\//i) != null) type = 'favorite';
  118. else if (title.match(/flickr.+pool$/i) != null) type = 'favorite';
  119. console.log("page type " + type);
  120.  
  121. return type;
  122. }
  123.  
  124. function showLink(index, elem)
  125. {
  126. var $e = $(elem);
  127. var photoId = $e.attr('data-photo-id');
  128. $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>');
  129.  
  130. var embedUrl = false;
  131. var linkUrl = false;
  132. var currentEmbedWidth = 100000;
  133. var currentEmbedHeight = 100000;
  134. var currentLinkWidth = 100000;
  135. var currentLinkHeight = 100000;
  136. var maxWidth = 0;
  137. var maxHeight = 0;
  138. var maxUrl = false;
  139.  
  140. for (var i = 0; i < source.length; i++) {
  141.  
  142. if (source[i].indexOf(photoId) == 40) {
  143.  
  144. var sizes = JSON.parse('{'+source[i]+'}');
  145. $.each(sizes.sizes, function( index, size ) {
  146. var width = parseInt(size.width);
  147. var height = parseInt(size.height);
  148.  
  149. if ((width >= 800 || height >= 600) && width < currentEmbedWidth && height < currentEmbedHeight) {
  150. embedUrl = size.url;
  151. currentEmbedHeight = height;
  152. currentEmbedWidth = width;
  153. }
  154. if ((width >= 2048 || height >= 1024) && width < currentLinkWidth && height < currentLinkHeight ) {
  155. linkUrl = size.url;
  156. currentLinkHeight = height;
  157. currentLinkWidth = width;
  158. }
  159. if (width > maxWidth && height > maxHeight) {
  160. maxWidth = width;
  161. maxHeight = height;
  162. maxUrl = size.url;
  163. }
  164.  
  165. });
  166.  
  167. if (!linkUrl) {
  168. linkUrl = maxUrl;
  169. }
  170. if (!embedUrl) {
  171. embedUrl = maxUrl;
  172. }
  173.  
  174. if (linkUrl && embedUrl) {
  175. var embedCode = '<a href="' + linkUrl + '" target="_blank" border="0"><img src="' + embedUrl + '" border="0"/></a>';
  176. $('#EMBED_'+photoId).val(embedCode);
  177. }
  178. }
  179. }
  180. }
  181.  
  182. /*
  183. * end support
  184. */
  185.  
  186. // Hide the Yahoo toolbar
  187. $('#eyebrow').hide();
  188. // Move the flickr toolbar to the top
  189. $('#global-nav').css('top', '0px');
  190.  
  191. var type = pageType();
  192.  
  193. if (type == 'photostream') {
  194. action_photostream();
  195. } else if (type == 'singlephoto') {
  196. action_singlephoto();
  197. }