|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name Gitlab PNG Compare
|
3 | 3 | // @namespace http://tampermonkey.net/
|
4 |
| -// @version 0.1 |
| 4 | +// @version 0.7 |
5 | 5 | // @description try to take over the world!
|
6 | 6 | // @author hannsen
|
7 | 7 | // @match https://git04.quodata.de/*/merge_requests/*
|
8 | 8 | // @grant none
|
9 |
| -// @downloadURL https://github.com/hannsen/userscripts/raw/master/Gitlab_PNG_Compare.user.js |
10 |
| -// @updateURL https://github.com/hannsen/userscripts/raw/master/Gitlab_PNG_Compare.user.js |
| 9 | +// @downloadURL https://git04.quodata.de/quodata/userscripts/-/raw/master/Gitlab_PNG_Compare.user.js |
| 10 | +// @updateURL https://git04.quodata.de/quodata/userscripts/-/raw/master/Gitlab_PNG_Compare.user.js |
11 | 11 | // ==/UserScript==
|
12 | 12 |
|
13 | 13 | (function() {
|
14 | 14 | 'use strict';
|
15 | 15 |
|
| 16 | + function onScreen (el) { |
| 17 | + return (jQuery(window).scrollTop() <= el.offset().top + el.height() && |
| 18 | + jQuery(window).scrollTop() + jQuery(window).height() >= el.offset().top); |
| 19 | + }; |
| 20 | + |
16 | 21 |
|
17 | 22 | function replaceImgView(){
|
18 |
| - jQuery('div.file-title:not(.qd-handled)').each(function(){ |
19 |
| - this.classList.add('qd-handled'); |
| 23 | + jQuery('div.diff-file.file-holder').each(function(){ |
20 | 24 | var $this = jQuery(this);
|
21 |
| - if($this.find('strong').text().trim().endsWith('.png')){ |
22 |
| - var $dl_links = $this.next().find('a[download]'); |
23 |
| - if(!$dl_links[0]){ |
24 |
| - $dl_links = $this.find('a.view-file'); |
25 |
| - } |
| 25 | + if(!onScreen($this)){ |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + var $diffView = $this.find('div.diff-viewer'); |
| 30 | + var $container = $this.parent().parent(); |
| 31 | + var datapath = $this.attr('data-path'); |
| 32 | + if(!datapath.endsWith('.png')){ |
| 33 | + setTimeout(function() { |
| 34 | + removeWronglyPlacedImgCompare($container); |
| 35 | + }, 50) |
| 36 | + return; |
| 37 | + } |
26 | 38 |
|
27 |
| - if($dl_links[0]){ |
28 |
| - var old_url = $dl_links[0].href.replace('/blob/', '/raw/'); |
29 |
| - var new_url = $dl_links[1] ? $dl_links[1].href.replace('/blob/', '/raw/') : old_url; |
30 |
| - $this.next().remove(); |
31 |
| - } |
32 | 39 |
|
33 |
| - var $img = buildImgCompare(old_url, new_url); |
34 |
| - $img.css('border', 'solid 1px black'); |
35 |
| - $this.after($img); |
| 40 | + var $imgcmp = $container.find('div.img_compare'); |
| 41 | + if($imgcmp[0] && $imgcmp.attr('data-path') == datapath){ |
| 42 | + return; |
36 | 43 | }
|
| 44 | + |
| 45 | + $imgcmp.remove(); |
| 46 | + |
| 47 | + var $diff_button = $this.find('button > .gl-button-text'); |
| 48 | + if($diff_button){ |
| 49 | + $diff_button.click(); |
| 50 | + } |
| 51 | + |
| 52 | + setTimeout(function() { |
| 53 | + startBuildImgCompare($this, datapath); |
| 54 | + }, 50) |
37 | 55 | });
|
38 | 56 | }
|
39 | 57 |
|
| 58 | + function removeWronglyPlacedImgCompare($container){ |
| 59 | + $container.find('div.img_compare').remove(); |
| 60 | + $container.find('div.diff-viewer').show(); |
| 61 | + } |
| 62 | + |
| 63 | + function startBuildImgCompare($this, datapath){ |
| 64 | + $this.find('div.diff-viewer').hide(); |
| 65 | + var $dl_links = $this.find('.gl-new-dropdown-item > a[role="menuitem"]'); |
| 66 | + if(!$dl_links[0]){ |
| 67 | + $dl_links = $this.find('a.view-file'); |
| 68 | + } |
| 69 | + |
| 70 | + if($dl_links[0]){ |
| 71 | + if(!$dl_links[1]){ |
| 72 | + // Lets check if there really is no old picture |
| 73 | + var $old_pic_link = $this.find('.diff-viewer .deleted a[data-qa-selector="download_button"]'); |
| 74 | + if($old_pic_link[0]){ |
| 75 | + $dl_links = [$old_pic_link[0], $dl_links[0]]; |
| 76 | + } |
| 77 | + } |
| 78 | + var old_url = $dl_links[0].href.replace('/blob/', '/raw/'); |
| 79 | + var new_url = $dl_links[1] ? $dl_links[1].href.replace('/blob/', '/raw/') : old_url; |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + var $img = buildImgCompare(old_url, new_url); |
| 85 | + $img.css('border', 'solid 1px black'); |
| 86 | + $img.attr('data-path', datapath); |
| 87 | + $this.after($img); |
| 88 | + } |
| 89 | + |
40 | 90 | function buildImgCompare(old_url, new_url){
|
41 |
| - var div = jQuery('<div><div/>'); |
| 91 | + var div = jQuery('<div class="img_compare"><div/>'); |
42 | 92 | var img_old = jQuery('<img class="img_old" src="' + old_url + '"/>');
|
43 | 93 | var img_new = jQuery('<img class="img_new" src="' + new_url + '"/>').css('display', 'none');
|
44 | 94 | var old_desc = "old< new";
|
|
64 | 114 | return div;
|
65 | 115 | }
|
66 | 116 |
|
67 |
| - window.onscroll = replaceImgView; |
| 117 | + var scrollTimeout; |
| 118 | + window.onscroll = function(){ |
| 119 | + clearTimeout(scrollTimeout); |
| 120 | + scrollTimeout = setTimeout(replaceImgView, 50); |
| 121 | + }; |
68 | 122 |
|
69 | 123 | })();
|
0 commit comments