Advertisement
decembre

GM - Flickr Cross-Recommendations - NEW Flickr (avr 2015)

Aug 17th, 2016
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Flickr Cross-Recommendations - NEW Flickr (avr 2015)
  3. // @namespace       http://netcetera.org
  4. // @include         http://www.flickr.com/photos/*
  5. // @include         https://www.flickr.com/photos/*
  6. // @match           http://www.flickr.com/photos/*
  7. // @match           https://www.flickr.com/photos/*
  8. // @version         1.53f
  9. // @grant           none
  10. // @author          Simon Whitaker (http://www.flickr.com/people/chubbybat/)
  11. // @contributor     Alesa Dam (http://www.flickr.com/people/alesadam/)
  12. // @contributor     Darren Greaves (http://www.flickr.com/people/boncey/)
  13. // @contributor     Martin Heimburger (http://www.flickr.com/people/Vispillo/)
  14. // @contributor     Mark Boyd (https://www.flickr.com/people/mark_boyd/)
  15. // @description     Adds a "People who faved this also faved..." panel to photos on Flickr
  16. // ==/UserScript==
  17.  
  18.  
  19. (function() {
  20. if (!document.location.href.match(/https?:\/\/www\.flickr\.com\/photos\/[^\/]+\/\d+.*/)) {
  21.     return;
  22. }
  23.    
  24. //var MAX_IMGS = 6;             // number of cross-recommendations to retrieve
  25. //var MAX_RUNS = MAX_IMGS * 15; // if we don't have those random images by then, better quit
  26.  
  27. var photo_count = 0;
  28. // DECEMBRE TWEAK 6 to 18 or 27
  29. var MAX_IMGS = 18;
  30.  
  31. var run_count = 0;
  32. var MAX_RUNS = MAX_IMGS * 15; // if we don't have those 6 /15 random images by then, better quit
  33.  
  34. var current_element;
  35. var prev_element;
  36. var target_element;
  37. var current_photo_id;
  38.    
  39. var photo_count;
  40. var run_count;
  41. var photos_to_ignore;
  42.  
  43. var testForElement = true;
  44.    
  45. var pageURLCheckTimer = setInterval (
  46.     function () {
  47.         if (    this.lastPathStr  !== location.pathname
  48.             ||  this.lastQueryStr !== location.search
  49.             ||  this.lastPathStr   === null
  50.             ||  this.lastQueryStr  === null
  51.         ) {
  52.             this.lastPathStr  = location.pathname;
  53.             this.lastQueryStr = location.search;
  54.             gmMain ();
  55.         } else if (testForElement) {
  56.            current_element = document.getElementsByClassName('view sub-photo-fave-view')[0];
  57.            if (!current_element) {
  58.                current_element = document.getElementsByClassName('fave-block')[0];
  59.            }
  60.            if (!current_element) {
  61.                current_element = document.getElementById('comments');
  62.            }
  63.            if ((current_element != prev_element) && (current_element)) {
  64.                prev_element = current_element;
  65.                testForElement = false;
  66.                              FCR_add_panel(current_element);
  67.            }
  68.         }
  69.     }
  70.     , 200
  71. );
  72.  
  73. function gmMain () {
  74.     if (!document.location.href.match(/https?:\/\/www\.flickr\.com\/.*/)) {
  75.                 clearInterval(pageURLCheckTimer);
  76.     } else {
  77.                 testForElement = document.location.href.match(/https?:\/\/www\.flickr\.com\/photos\/[^\/]+\/\d+.*/);
  78.         }
  79. }
  80.    
  81. var protocol = window.location.protocol;
  82.  
  83. if (typeof(GM_log) == "undefined") { // Chrome
  84.     GM_log = function(message) {
  85.         console.info('FCR: ' + message);
  86.     }
  87. }
  88.  
  89. var getJSVariable = function (regex) {
  90.         // Thanks to Vispillo for this compact code
  91.         var retval;
  92.         var scripts = document.getElementsByTagName('script');
  93.     for(var i = 0, len = scripts.length; i < len; ++i) {
  94.         var script = scripts[i];
  95.             var html = script.innerHTML;
  96.             try {
  97.                 retval = regex.exec(html)[1];
  98.         break;
  99.             } catch (e) {
  100.             }
  101.         }
  102.         return retval;
  103. }
  104.    
  105. function pickRandomFavoriteFrom(people) {
  106.     if (!people || people.length == 0) {
  107.         GM_log("no people found");
  108.         return;
  109.     }
  110.     if (photo_count >= MAX_IMGS) { // sanity check
  111.         return;
  112.     }
  113.     if (++run_count > MAX_RUNS) {
  114. GM_log("out of data");
  115.         return;
  116.     }
  117.     photos_to_ignore[current_photo_id] = 1;
  118.    
  119.     var personIdx = Math.floor(Math.random() * people.length);
  120.     var nsid = people[personIdx].nsid;
  121.        
  122.     var listRequest = new XMLHttpRequest();
  123.     listRequest.onload = function(getPublicListResponse) {
  124.             var listJson = listRequest.response;
  125.             try {
  126.             var data = JSON.parse(listJson);
  127.             } catch (e) {
  128.             try {
  129.                 data   = eval('(' + listJson + ')');
  130.             } catch (f) {
  131.                 //GM_log("exception parsing list response: " + e);
  132.                 pickRandomFavoriteFrom(people);
  133.                 return;
  134.             }
  135.             }
  136.             var photos = data.photos.photo;
  137.            
  138.             var photoIdx = Math.floor(Math.random() * photos.length);
  139.             var photo  = photos[photoIdx];
  140.             if (typeof photo == 'undefined') {
  141.                 pickRandomFavoriteFrom(people);
  142.                 return;
  143.             }
  144.             if (photos_to_ignore[photo.id]) {
  145.                 pickRandomFavoriteFrom(people);
  146.                 return;
  147.             }
  148.             photos_to_ignore[photo.id] = 1;
  149.            
  150.             var photo_url = protocol + '//farm' + photo.farm
  151.                       + '.static.flickr.com/'
  152.                       + photo.server + '/'
  153.                       + photo.id + '_' + photo.secret + '_s.jpg';
  154.            
  155.             var page_url  = protocol+'//www.flickr.com/photos/'
  156.                       + photo.owner
  157.                       + '/' + photo.id + '/';
  158.                        
  159.             var img = document.createElement('img');
  160.             img.setAttribute('src',photo_url);
  161.             img.setAttribute('border', 0);
  162.            
  163.             var a = document.createElement('a');
  164.             a.setAttribute('href', page_url);
  165.             a.setAttribute('target', "_blank");
  166.             a.setAttribute('title', photo.title);
  167.             a.style.marginRight = '8px';
  168.             a.appendChild(img);
  169.            
  170.             document.getElementById('FCR_photos').appendChild(a);
  171.            
  172.             // Show the main div, in case it's still hidden
  173.             document.getElementById('FCR_main').style.display = "block";
  174.            
  175.             ++photo_count;
  176.             pickRandomFavoriteFrom(people);
  177.         };
  178.     listRequest.open('get', protocol+'//api.flickr.com/services/rest/'
  179.                +'?method=flickr.favorites.getPublicList'
  180.                +'&api_key=' + api_key
  181.                +'&format=json&nojsoncallback=1'
  182.                +'&user_id=' + nsid,
  183.         true);
  184.     listRequest.send();
  185. }
  186.  
  187. function FCR_add_panel(the_element) {
  188. try {
  189.     photo_count = 0;
  190.     run_count = 0;
  191.     photos_to_ignore = new Array();    
  192.     if (!the_element) {
  193.        GM_log("element not found; aborting");
  194.        return;
  195.     }
  196.     current_photo_id = re.exec(document.location.href)[1];
  197.     var xrec_div     = document.createElement('div');
  198.     xrec_div.setAttribute('id', 'FCR_main');
  199.     var xrec_photos  = document.createElement('div');
  200.     xrec_photos.setAttribute('id', 'FCR_photos');
  201.     var xrec_header  = document.createElement('h3');
  202.     xrec_header.innerHTML = 'People who faved this also faved...';
  203.    
  204.     xrec_div.appendChild(xrec_header);
  205.     xrec_div.appendChild(xrec_photos);
  206.     the_element.parentNode.insertBefore(xrec_div, the_element);
  207.  
  208.     xrec_div.style.display = 'none';
  209.  
  210.     var url =  protocol+'//api.flickr.com/services/rest/'
  211.                +'?method=flickr.photos.getFavorites'
  212.                +'&api_key=' + api_key
  213.                +'&format=json&nojsoncallback=1'
  214.                +'&photo_id=' + current_photo_id;
  215.    
  216.  
  217.     var favesRequest = new XMLHttpRequest();
  218.     favesRequest.onload = function(response) {
  219.         var json = favesRequest.response;
  220.         try {
  221.         var data = JSON.parse(json);
  222.         } catch (e) {
  223.         try {
  224.             data = eval('(' + json + ')');
  225.         } catch (f) {
  226.             GM_log("exception parsing response: " + e);
  227.             return;
  228.         }
  229.         }
  230.             pickRandomFavoriteFrom(data.photo.person);
  231.            
  232.         };
  233.     favesRequest.open('get', url, true);
  234.     favesRequest.send();
  235.  
  236. } catch (e) {
  237.     GM_log("Exception occurred: " + e);
  238. }
  239. }
  240.  
  241.     var re = /flickr\.com\/photos\/[^\/]+\/(\d+).*/;
  242.     var api_key = '45d5d4b7dff9bc653c8eb3e73271c10c';
  243.  
  244. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement