-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
156 lines (144 loc) · 6.65 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
let arrowRight = chrome.extension.getURL('right-arrow.svg');
let arrowLeft = chrome.extension.getURL('left-arrow.svg');
function mainFunc() {
// clear history
let href = window.location.href.match(/forum\?id=(.[^&^#]*)(&.*)?(#.*)?/);
if (href == null) {}
else {
let id = href.at(1);
fetch('https://api2.openreview.net/notes?forum=' + id, {
method: 'GET',
headers: {'Content-Type': 'application/json; charset=utf-8'},
credentials: 'include',
})
.then(response => response.json())
.then(data1 => {
if (data1 === null || data1.notes.length === 0) {
return fetch('https://api.openreview.net/notes?forum=' + id, {
method: 'GET',
headers: {'Content-Type': 'application/json; charset=utf-8'},
credentials: 'include',
})
.then(response => response.json())
.then(data2 => {
parseData(
data2.notes.filter(e => e.replyto === id && e.invitation.toLowerCase().includes('official_review')).sort((a, b) => b.number - a.number),
data2.notes.filter(e => e.replyto === id && e.invitation.toLowerCase().includes('decision') && 'content' in e ? 'decision' in e.content : false),
);
})
.catch((error) => {console.error('Error: ' + error)})
} else {
parseData(
data1.notes.filter(e => e.replyto === id && e.invitations[0].toLowerCase().includes('official_review')).sort((a, b) => b.number - a.number),
data1.notes.filter(e => e.replyto === id && e.invitations[0].toLowerCase().includes('decision') && 'content' in e ? 'decision' in e.content : false),
);
}
})
.catch((error) => {console.error('Error: ' + error)})
}
}
function parseData(reviews, decision) {
if (reviews.length === 0) {return;}
// create new div
let sumNav = document.createElement('div');
sumNav.className = 'sum-nav';
// parse data
let ratings = [];
let isNumber = null;
let tableStr = "<table id='sum-tbl' style='display: table'><tr><td class='scd' colspan='2'><a href='https://github.com/weigq/openview_quicklook'>Quick👀Look</a></td></tr>";
reviews.forEach((elem, i) => {
let f = null;
if (elem.content.rating != null) {
// neurips/icml -> 10: xxx...
f = elem.content.rating.value ? elem.content.rating.value : elem.content.rating;
} else if (elem.content.recommendation != null) {
// iclr 2023 -> 10: xxx...
f = elem.content.recommendation;
} else if (elem.content.preliminary_rating != null) {
// acmmm -> accept: xxx.
f = elem.content.preliminary_rating;
} else if (elem.content.overall_recommendation != null) {
// cvpr -> 5: xxx.
f = elem.content.overall_recommendation.value ? elem.content.overall_recommendation.value : elem.content.overall_recommendation;
} else if (elem.content.final_rating != null) {
f = elem.content.final_rating;
}
if (f == null) {}
else {
let rating;
// get rating
if (typeof f === "number") {
rating = f.toString();
} else if (f.match(/(.*?):/) != null) {
rating = f.match(/(.*?):/)[1];
} else if (f.match(/(.*?)-/) != null) {
rating = f.match(/(.*?)-/)[1];
} else {
rating = null;
}
// if rating is number
if (isNumber == null && rating != null && rating.match(/\d*/)[0].length === rating.length) {
isNumber = true;
}
tableStr += "<tr class='clk-tr' data-href=" + elem.id + "><td class='fst'>R" + (i + 1) + ":</td><td class='scd'>" + rating + "</td></tr>";
ratings.push(rating);
}
});
// if rating is number
if (isNumber === true) {
let avg_rating = ratings.reduce((a, b) => parseInt(a, 10) + parseInt(b, 10)) / ratings.length;
tableStr += "<tr class='last'><td class='fst'>avg:</td><td class='scd'>" + avg_rating.toFixed(2) + "</td></tr>";
}
// decision
if (decision.length > 0) {
decision_value = decision[0].content.decision.value ? decision[0].content.decision.value : decision[0].content.decision;
tableStr += "<tr class='last'><td colspan='2' class='scd'>" + decision_value + "</td></tr>";
}
tableStr += "</table>";
tableStr += "<div class='nav-btn'><img id='nav-arrow' src=" + arrowLeft + "></div>"
sumNav.insertAdjacentHTML('afterbegin', tableStr);
document.body.append(sumNav);
addClick();
}
const getOffsetTop = element => {
let offsetTop = 0;
while(element) {
offsetTop += element.offsetTop;
element = element.offsetParent;
}
return offsetTop;
}
function addClick() {
let trs = document.querySelectorAll('.clk-tr');
trs.forEach((e, i) => {
e.addEventListener('click', () => {
let href = e.getAttribute('data-href');
let elem;
if (document.querySelector("[data-id='" + href + "']") != null) {
elem = document.querySelector("[data-id='" + href + "']"); // neurips23
} else {
elem = document.getElementById("note_" + href); // before neurips23
}
let y = getOffsetTop(elem);
window.scroll({top: y - 75, behavior: 'smooth' });
})
})
let arrow = document.getElementById('nav-arrow');
arrow.addEventListener('click', () => {
let sumTbl = document.getElementById('sum-tbl');
sumTbl.getBoundingClientRect().left >= 0 ? (arrow.src = arrowRight, sumTbl.style.transform = 'translateX(-200%)') : (arrow.src = arrowLeft, sumTbl.style.transform = 'translateX(0)');
})
}
// clear history
function clearSummary() {
let sumNav = document.getElementsByClassName('sum-nav');
while (sumNav.length > 0) {
sumNav[0].parentNode.removeChild(sumNav[0]);
}
}
// tab update
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.message === 'TabUpdated') { clearSummary(); mainFunc(); }
})
// leaving tab
window.addEventListener('beforeunload', function(e) { clearSummary() })