Skip to content

Commit f411135

Browse files
authored
Update Gitlab_PNG_Compare.user.js
1 parent 02ba8c5 commit f411135

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

Gitlab_PNG_Compare.user.js

+74-20
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,94 @@
11
// ==UserScript==
22
// @name Gitlab PNG Compare
33
// @namespace http://tampermonkey.net/
4-
// @version 0.1
4+
// @version 0.7
55
// @description try to take over the world!
66
// @author hannsen
77
// @match https://git04.quodata.de/*/merge_requests/*
88
// @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
1111
// ==/UserScript==
1212

1313
(function() {
1414
'use strict';
1515

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+
1621

1722
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(){
2024
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+
}
2638

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-
}
3239

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;
3643
}
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)
3755
});
3856
}
3957

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+
4090
function buildImgCompare(old_url, new_url){
41-
var div = jQuery('<div><div/>');
91+
var div = jQuery('<div class="img_compare"><div/>');
4292
var img_old = jQuery('<img class="img_old" src="' + old_url + '"/>');
4393
var img_new = jQuery('<img class="img_new" src="' + new_url + '"/>').css('display', 'none');
4494
var old_desc = "old< new";
@@ -64,6 +114,10 @@
64114
return div;
65115
}
66116

67-
window.onscroll = replaceImgView;
117+
var scrollTimeout;
118+
window.onscroll = function(){
119+
clearTimeout(scrollTimeout);
120+
scrollTimeout = setTimeout(replaceImgView, 50);
121+
};
68122

69123
})();

0 commit comments

Comments
 (0)