Advertisement
Sixem

Userscript: Dvach (2ch.hk) Image Options (Reverse + DDL)

Apr 14th, 2018
2,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Dvach Image Options
  3. // @version      0.2
  4. // @description  Adds direct download button and reverse image search options
  5. // @author       Π΅ΠΌΡƒ
  6. // @match        https://2ch.hk/*/res/*
  7. // @require      https://code.jquery.com/jquery-3.2.1.min.js
  8. // @resource     fa_CSS https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css
  9. // @grant        GM_addStyle
  10. // @grant        GM_getResourceText
  11. // ==/UserScript==
  12.  
  13. this.$ = this.jQuery = jQuery.noConflict(true);
  14.  
  15. GM_addStyle(GM_getResourceText("fa_CSS"));
  16.  
  17. GM_addStyle(".image-reverse { text-decoration:none;margin-left:4px;color:#666666;font-size:10px; } .image-reverse:hover { text-decoration: underline; } .image-direct-download { text-decoration:none;margin-left:4px;font-size:14px; }");
  18.  
  19. function getReverseImageSearchOptions(url)
  20. {
  21.     var options = {
  22.         'google': 'https://www.google.com/searchbyimage?image_url=' + url + '&safe=off',
  23.         'yandex': 'https://www.yandex.com/images/search?rpt=imageview&img_url=' + url,
  24.         'iqdb': 'https://iqdb.org/?url=' + url
  25.     };
  26.  
  27.     return options;
  28. }
  29.  
  30. var re = new RegExp('(mp4|webm)');
  31.  
  32. function onElementHeightChange(elm, callback)
  33. {
  34.     var lastHeight = elm.clientHeight, newHeight;
  35.     (function run()
  36.     {
  37.         newHeight = elm.clientHeight;
  38.         if(lastHeight != newHeight)
  39.             callback();
  40.         lastHeight = newHeight;
  41.  
  42.         if(elm.onElementHeightChangeTimer)
  43.             clearTimeout(elm.onElementHeightChangeTimer);
  44.  
  45.         elm.onElementHeightChangeTimer = setTimeout(run, 1000);
  46.     })();
  47. }
  48.  
  49. function applyOptions()
  50. {
  51.  
  52.   $(".file-attr").each(function(index)
  53.   {
  54.  
  55.     if($(this).find('.image-options').length > 0)
  56.     {
  57.         return;
  58.     }
  59.  
  60.     var file_url = $(this).find('a');
  61.  
  62.     if(file_url != undefined)
  63.     {
  64.         $reverse_options = getReverseImageSearchOptions(document.location.origin + file_url.attr('href'));
  65.  
  66.         if (re.test(file_url.attr('href'))) {
  67.           $preview = $(this).parent().find('.preview');
  68.  
  69.           if($preview.length > 0)
  70.           {
  71.             if($preview.attr('src') != undefined)
  72.             {
  73.               $reverse_options = getReverseImageSearchOptions(document.location.origin + $preview.attr('src'));
  74.             }
  75.           }
  76.         }
  77.  
  78.         var first_c = $(this).find(">:first-child");
  79.  
  80.         var direct_download = '<a class="fa fa-download image-direct-download" href="' + file_url.attr('href')  + '" download="' + file_url.attr('title') + '"></a>';
  81.         var r_google = '<a target="_blank" href="' + $reverse_options['google']  + '" class="image-reverse">google</a>';
  82.         var r_yandex = '<a target="_blank" href="' + $reverse_options['yandex']  + '" class="image-reverse">yandex</a>';
  83.  
  84.         first_c.after('<div class="image-options" style="display:inline-block;">' + direct_download + r_google + r_yandex + '</div>');
  85.  
  86.     }
  87.   });
  88. }
  89.  
  90. $(document).ready(function()
  91. {
  92.  
  93.   onElementHeightChange(document.body, function(){
  94.     applyOptions();
  95.   });
  96.  
  97.   applyOptions();
  98.  
  99. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement