Skip to content

Commit 1a6b055

Browse files
committed
+options & storage, clean up, fixes
1 parent 6b2a285 commit 1a6b055

File tree

7 files changed

+118
-19
lines changed

7 files changed

+118
-19
lines changed

background.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function openSettings() {
2+
browser.runtime.openOptionsPage();
3+
}
4+
5+
browser.browserAction.onClicked.addListener(openSettings);

icons/icon-16.png

269 Bytes
Loading

icons/icon-32.png

724 Bytes
Loading

manifest.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
{
22
"manifest_version": 2,
3-
"name": "Hacktoberfest Tag Detector",
3+
"name": "Hacktoberfest Opt-in Checker",
44
"version": "1.0",
55
"description": "Detect whether the repository has opted into the 2020 Hacktoberfest.",
66
"icons": {
77
"96": "icons/icon-96.png",
88
"48": "icons/icon-48.png"
99
},
1010
"permissions": [
11+
"storage",
1112
"https://api.github.com/*"
1213
],
1314
"content_scripts": [
1415
{
1516
"matches": [
16-
"*://github.com/*"
17+
"*://github.com/*",
18+
"*://www.github.com/*"
1719
],
1820
"js": [
1921
"optindetector.js"
2022
]
2123
}
22-
]
24+
],
25+
"options_ui": {
26+
"page": "settings/options.html"
27+
},
28+
"browser_specific_settings": {
29+
"gecko": {
30+
31+
}
32+
},
33+
"background": {
34+
"scripts": [
35+
"background.js"
36+
]
37+
},
38+
"browser_action": {
39+
"default_icon": {
40+
"16": "icons/icon-16.png",
41+
"32": "icons/icon-32.png"
42+
}
43+
}
2344
}

optindetector.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
function checkIfEnrolled() {
2-
console.log("Requested tag check")
2+
console.log("[Hacktoberfest Tag Detector] Check started")
3+
let GITHUB_API_USERNAME;
4+
let GITHUB_API_TOKEN;
5+
6+
try {
7+
GITHUB_API_USERNAME = browser.storage.sync.get("username");
8+
GITHUB_API_TOKEN = browser.storage.sync.get("token");
9+
} catch {
10+
alert("[Hacktoberfest Tag Detector] Please enter username/token in the settings page!");
11+
browser.runtime.openSettingsPage();
12+
return;
13+
}
14+
15+
if (GITHUB_API_USERNAME.length < 1 || GITHUB_API_TOKEN.length < 1) {
16+
alert("[Hacktoberfest Tag Detector] Please enter username/token in the settings page!");
17+
browser.runtime.openSettingsPage();
18+
return;
19+
}
20+
321
let headers = new Headers();
4-
headers.append('Authorization', 'Basic ', btoa(`{GITHUB_API_USERNAME}:{GITHUB_API_TOKEN}`))
22+
headers.append('Authorization', 'Basic ', btoa(`{GITHUB_API_USERNAME}:{GITHUB_API_TOKEN}`));
523

624
const targetItems = document.querySelector("[data-tab-item='code-tab']").attributes['href'].value.split("/")
725
const user = targetItems[1]
826
const repo = targetItems[2]
9-
console.log(targetItems);
10-
console.log(`Test user: ${user}, ${repo}`)
11-
console.log("TEST");
1227

1328
fetch("https://api.github.com/repos/" + user + "/" + repo + "/labels", {
1429
method: 'GET',
1530
headers: headers
1631
})
1732
.then(response => response.json())
1833
.then(json => {
19-
console.log(json);
20-
labelNames = json.map(i => i.name)
21-
const isHacktoberfestRepo = "hacktoberfest" in labelNames || "hacktoberfest-accepted" in labelNames
34+
const labelNames = json.map(i => i.name);
35+
const isHacktoberfestRepo = labelNames.includes("hacktoberfest") || labelNames.includes("hacktoberfest-accepted");
2236
document.body.style.border = isHacktoberfestRepo ? "5px solid green" : "5px solid blue";
2337
console.log(isHacktoberfestRepo ? "Hacktoberfest Tag Detector - Opted-in repository" : "Hacktoberfest Tag Detector - Not opted-in repository")
2438
})
2539
.catch(e => console.log(e))
2640

2741
}
2842

29-
let isGithubProjectPage = false;
30-
try {
31-
isGithubProjectPage = document.querySelector(".application-main").children[0].attributes["itemtype"].value === "http://schema.org/SoftwareSourceCode"
32-
} catch (e) {
33-
isGitHubProjectPage = false;
43+
44+
function isGithubProject() {
45+
try {
46+
if (document.querySelector(".application-main").children[0].attributes.itemtype.value === "http://schema.org/SoftwareSourceCode")
47+
return true;
48+
else
49+
return false;
50+
} catch (e) {
51+
return false;
52+
}
3453
}
3554

36-
if (isGithubProjectPage) {
37-
console.log("Hacktoberfest Tag Detector - GitHub Project detected");
55+
56+
if (isGithubProject()) {
57+
console.log("[Hacktoberfest Tag Detector] GitHub Project detected");
3858
document.addEventListener("keyup", function (e) {
3959
var key = e.key || e.keyCode;
4060
if (key === 'h' || key === 'KeyH' || key === 72) {
4161
checkIfEnrolled()
4262
}
4363
});
4464
} else {
45-
console.log("Hacktoberfest Tag Detector - GitHub Project not detected");
65+
console.log("[Hacktoberfest Tag Detector] GitHub Project not detected");
4666
}

settings/options.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
<form>
11+
<label>Github Username*: <input type="text" id="username" ></label>
12+
<label>Github Token*: <input type="text" id="token" ></label>
13+
<button type="submit">Save</button>
14+
</form>
15+
16+
<p>Token = Personal Access Token. Can be generated from Settings -> Developer Settings -> Personal Access Tokens. No permissions should be enabled from the list. Token used simply to query the API.</p>
17+
18+
<script src="options.js"></script>
19+
20+
</body>
21+
22+
</html>

settings/options.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function saveOptions(e) {
2+
e.preventDefault();
3+
browser.storage.sync.set({
4+
username: document.querySelector("#username").value,
5+
token: document.querySelector("#token").value
6+
});
7+
}
8+
9+
function restoreOptions() {
10+
11+
function setCurrentChoiceUsername(result) {
12+
document.querySelector("#username").value = result.username || "";
13+
}
14+
15+
function setCurrentChoiceToken(result) {
16+
document.querySelector("#token").value = result.token || "";
17+
}
18+
19+
function onError(error) {
20+
console.log(`Error: ${error}`);
21+
}
22+
23+
let gettingUsername = browser.storage.sync.get("username");
24+
gettingUsername.then(setCurrentChoiceUsername, onError);
25+
26+
let gettingToken = browser.storage.sync.get("token");
27+
gettingToken.then(setCurrentChoiceToken, onError);
28+
}
29+
30+
document.addEventListener("DOMContentLoaded", restoreOptions);
31+
document.querySelector("form").addEventListener("submit", saveOptions);

0 commit comments

Comments
 (0)