Skip to content

Commit bed1281

Browse files
Merge pull request #18 from trevorcampbell/jquery-waitload-nohighlight
Handling pages with no jquery, removing highlights, waiting for page load
2 parents 421e054 + e314a58 commit bed1281

File tree

4 files changed

+72
-48
lines changed

4 files changed

+72
-48
lines changed

website_diff/crawler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def crawl(filepath, gathered, content_selector = 'html', crawled = None):
4242
for link in elem.find_all('a'):
4343
# extract the url
4444
ref = link.get('href')
45+
# skip <a> tags without href properties
46+
if ref is None:
47+
continue
4548
# remove anchors
4649
ref = ref.split('#')[0]
4750
# parse the url

website_diff/page.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def highlight_links(file, root, add_pages, del_pages, diff_pages):
102102
for link in soup.select('a'):
103103
# extract the url
104104
ref = link.get('href')
105+
# skip <a> tags without href properties
106+
if ref is None:
107+
continue
105108
# remove anchors
106109
ref = ref.split('#')[0]
107110
# parse the url

website_diff/static/website_diff.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ del.diff, del.diff * {
44
ins.diff, ins.diff * {
55
background-color: lightgreen;
66
}
7-
del.diff-selected, del.diff-selected * {
7+
/*del.diff-selected, del.diff-selected * {
88
background-color: firebrick;
99
}
1010
ins.diff-selected, ins.diff-selected * {
1111
background-color: limegreen;
12-
}
12+
}*/
1313

1414
del.diff:has(img) {
1515
background-color: transparent;
@@ -27,13 +27,14 @@ ins.diff > img {
2727
border: 0.7rem solid lightgreen !important;
2828
}
2929

30+
/*
3031
del.diff-selected > img {
3132
border: 0.7rem solid firebrick !important;
3233
}
3334
3435
ins.diff-selected > img {
3536
border: 0.7rem solid limegreen !important;
36-
}
37+
}*/
3738

3839
.link-to-diff {
3940
color: darkgoldenrod !important;

website_diff/static/website_diff.js

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// dynamically load jQuery if not already present on the website
2+
if (typeof jQuery == 'undefined') {
3+
document.write('<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>');
4+
}
5+
16
var diffidx = 0;
27

3-
$(document).ready(function() {
4-
// highlight and scroll to the first diff element
5-
var cur;
6-
cur = $(".diff:visible").eq(diffidx)
7-
cur.addClass("diff-selected");
8-
$(cur)[0].scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest"});
8+
window.addEventListener('load', () => {
9+
scrollToFirstDiff(diffidx);
10+
scrollToNextDiff(diffidx);
911
});
1012

1113
// Borrowed from https://stackoverflow.com/a/15203639
@@ -29,51 +31,66 @@ function isElementVisible(el) {
2931
);
3032
}
3133

32-
$(document).on("keypress", function(e) {
33-
var cur;
34-
var next;
35-
cur = $(".diff:visible").eq(diffidx)
36-
cur.removeClass("diff-selected");
37-
if (e.which == 110){
38-
39-
var diffidx_tmp = diffidx + 1;
40-
if (diffidx_tmp > $(".diff:visible").length-1){
41-
diffidx_tmp = $(".diff:visible").length-1;
42-
}
43-
var cur_tmp = $(".diff:visible").eq(diffidx_tmp);
34+
// Scroll to the center of first diff element on page
35+
function scrollToFirstDiff(diffidx) {
36+
var cur;
37+
cur = $(".diff:visible").eq(diffidx)
38+
if (typeof cur[0] != "undefined") {
39+
cur.addClass("diff-selected");
40+
$(cur)[0].scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest"});
41+
}
42+
}
4443

45-
while (isElementVisible(cur_tmp[0])) {
46-
diffidx_tmp += 1;
47-
if (diffidx_tmp > $(".diff:visible").length-1){
48-
diffidx_tmp = $(".diff:visible").length-1;
49-
break
50-
}
51-
cur_tmp = $(".diff:visible").eq(diffidx_tmp)
44+
// Scroll to the next diff element on pressing n, previous on pressing Shift + n
45+
function scrollToNextDiff(diffidx) {
46+
$(document).on("keypress", function(e) {
47+
var cur;
48+
var next;
49+
cur = $(".diff:visible").eq(diffidx)
50+
if (typeof cur[0] == "undefined") {
51+
return;
5252
}
53-
cur = cur_tmp
54-
diffidx = diffidx_tmp
55-
}
56-
if (e.which == 78) {
53+
cur.removeClass("diff-selected");
54+
if (e.which == 110){
5755

58-
var diffidx_tmp = diffidx - 1;
59-
if (diffidx_tmp < 0){
60-
diffidx_tmp = 0;
56+
var diffidx_tmp = diffidx + 1;
57+
if (diffidx_tmp > $(".diff:visible").length-1){
58+
diffidx_tmp = $(".diff:visible").length-1;
6159
}
6260
var cur_tmp = $(".diff:visible").eq(diffidx_tmp);
63-
61+
6462
while (isElementVisible(cur_tmp[0])) {
65-
diffidx_tmp -= 1;
66-
if (diffidx_tmp < 0){
67-
diffidx_tmp = 0;
68-
break
69-
}
70-
cur_tmp = $(".diff:visible").eq(diffidx_tmp)
63+
diffidx_tmp += 1;
64+
if (diffidx_tmp > $(".diff:visible").length-1){
65+
diffidx_tmp = $(".diff:visible").length-1;
66+
break
67+
}
68+
cur_tmp = $(".diff:visible").eq(diffidx_tmp)
7169
}
7270
cur = cur_tmp
7371
diffidx = diffidx_tmp
74-
}
75-
next = $(".diff:visible").eq(diffidx)
76-
next.addClass("diff-selected")
77-
$(next)[0].scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest"});
78-
});
79-
72+
}
73+
if (e.which == 78) {
74+
75+
var diffidx_tmp = diffidx - 1;
76+
if (diffidx_tmp < 0){
77+
diffidx_tmp = 0;
78+
}
79+
var cur_tmp = $(".diff:visible").eq(diffidx_tmp);
80+
81+
while (isElementVisible(cur_tmp[0])) {
82+
diffidx_tmp -= 1;
83+
if (diffidx_tmp < 0){
84+
diffidx_tmp = 0;
85+
break
86+
}
87+
cur_tmp = $(".diff:visible").eq(diffidx_tmp)
88+
}
89+
cur = cur_tmp
90+
diffidx = diffidx_tmp
91+
}
92+
next = $(".diff:visible").eq(diffidx)
93+
next.addClass("diff-selected")
94+
$(next)[0].scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest"});
95+
});
96+
}

0 commit comments

Comments
 (0)