-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
113 lines (99 loc) · 3.38 KB
/
userscript.js
File metadata and controls
113 lines (99 loc) · 3.38 KB
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
// ==UserScript==
// @name License indicator
// @namespace https://github.com/isirode
// @match https://github.com/orgs/*/repositories
// @match https://github.com/search*
// @match https://github.com/*
// @grant none
// @version 1.0
// @author Onésime Deleham
// @description This userscript allow to indicate strongly wether or not a Github repository is open-source in places where they are listed
// ==/UserScript==
console.log("Starting Github annotation");
setInterval(annotateRepositories, 500);
function annotateRepositories() {
const annotationsAlreadyThere = document.querySelector('.license-indicator');
if (annotationsAlreadyThere) {
console.log("Annotations already there");
return;
}
if (window.location.href.includes('github.com/search')) {
annotateRepoListItem();
} else if (window.location.href.includes('repositories')) {
annotateBoxRow();
} else if ((window.location.pathname.match(/\//g) || []).length === 1) {
annotateBoxRow();
// TODO : if Github start indicating the license in the Box elements, add them here (top of a user's page)
} else {
console.error("unknown url")
console.log(window.location.pathname)
}
}
function annotateBoxRow() {
console.log("annotate box-row")
let nodeList = document.querySelectorAll('.Box-row');
if (!nodeList || nodeList.length === 0) {
console.log("toto");
// Info : Github element is different if you list your own repository
nodeList = document.querySelectorAll('li.public');
if (!nodeList || nodeList.length === 0) {
return
}
}
for (let i = 0; i < nodeList.length; i++) {
let licenseNode = nodeList[i].querySelector('.octicon-law');
if (licenseNode) {
annotateOpenSource(nodeList[i]);
} else {
annotateNotOpenSource(nodeList[i]);
}
}
}
function annotateRepoListItem() {
const nodeList = document.querySelectorAll('.repo-list-item');
if (!nodeList) {
return;
}
for (let i = 0; i < nodeList.length; i++) {
let potentialNodes = nodeList[i].querySelectorAll('.mr-3');
let isOpenSource = false
if (potentialNodes) {
for (let j = 0; j < potentialNodes.length; j++) {
if (isOpenSource) break;
let text = potentialNodes[j].childNodes[0].nodeValue;
// Info : Github does not provide class indicators here
switch (text.trim()) {
case 'MIT license':
case 'GPL-3.0 license':
case 'Apache-2.0 license':
case 'BSD-3-Clause license':
case 'CC0-1.0 license':
case 'GNU Affero General Public License v3.0':
case 'Other':
isOpenSource = true;
console.log('OK')
break;
default:
isOpenSource = false;
}
}
}
if (isOpenSource) {
annotateOpenSource(nodeList[i]);
} else {
annotateNotOpenSource(nodeList[i]);
}
}
}
function annotateOpenSource(node) {
let elem = document.createElement('span')
elem.classList.add('license-indicator')
elem.innerHTML += "<strong style='color: green'>open-source</strong>"
node.prepend(elem)
}
function annotateNotOpenSource(node) {
let elem = document.createElement('span')
elem.classList.add('license-indicator')
elem.innerHTML += "<strong style='color: red'>not open-source</strong>"
node.prepend(elem)
}