Skip to content

Commit 59ba50c

Browse files
authored
Merge pull request #14 from namila007/master
Working notifications
2 parents 951f9d1 + 33a17f4 commit 59ba50c

File tree

15 files changed

+395
-17
lines changed

15 files changed

+395
-17
lines changed

background/authorize.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* exported getAccessToken */
2+
3+
const REDIRECT_URL = browser.identity.getRedirectURL();
4+
const CLIENT_ID = "12428";
5+
const KEY = "f26RUH3uoCiokrEYNeDf9Q(("
6+
const SCOPES = ["read_inbox","private_info"];
7+
const AUTH_URL =
8+
`https://stackoverflow.com/oauth/dialog?client_id=${CLIENT_ID}&key=${KEY}&redirect_uri=${REDIRECT_URL}&scope=${encodeURIComponent(SCOPES.join(' '))}`;
9+
const VALIDATION_BASE_URL="https://api.stackexchange.com/2.2/";
10+
11+
function extractAccessToken(redirectUri) {
12+
let m = redirectUri.match(/[#?](.*)/);
13+
if (!m || m.length < 1)
14+
return null;
15+
let params = new URLSearchParams(m[1].split("#")[0]);
16+
return params.get("access_token");
17+
}
18+
19+
function validate(redirectURL) {
20+
const accessToken = extractAccessToken(redirectURL);
21+
//console.log(accessToken + " access")
22+
if (!accessToken) {
23+
throw "Authorization failure";
24+
}
25+
const validationURL = `${VALIDATION_BASE_URL}access-tokens/${accessToken}?key=${KEY}`;
26+
27+
//console.log(validationURL)
28+
return checkResponse(validationURL)
29+
30+
31+
}
32+
33+
function checkResponse(validationURL) {
34+
return axios.get(validationURL)
35+
.then(res => {
36+
// console.log(res.data.items[0].access_token)
37+
if (res.data.items && res.data.items.length > 0) {
38+
return Promise.resolve(res.data.items[0].access_token);
39+
}
40+
41+
throw new Error("Access token expired");
42+
})
43+
}
44+
45+
async function authorize() {
46+
return await browser.identity.launchWebAuthFlow({
47+
interactive: true,
48+
url: AUTH_URL
49+
});
50+
}
51+
52+
function getAccessToken() {
53+
return authorize().then(validate);
54+
}

background/axios.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

background/inboxInfo.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function notifyunread(count) {
2+
browser.notifications.create({
3+
"type": "basic",
4+
"iconUrl": browser.extension.getURL("icons/addon.png"),
5+
"title": "Inbox",
6+
"message": "You have "+count+" unread messages"
7+
})
8+
}
9+
10+
11+
12+
function unreadmessages(token){
13+
var url = INBOX_READ+`&access_token=${token}`
14+
console.log(url)
15+
16+
return axios.get(url).then(res=>{
17+
console.log(totunread+" "+res.data.items.length )
18+
console.log(res.data)
19+
if(totunread < res.data.items.length )
20+
{
21+
totunread = res.data.items.length
22+
console.log(totunread+" "+res.data.items.length )
23+
return Promise.resolve(res.data.items.length)
24+
}
25+
if(totunread == res.data.items.length ){
26+
console.log(totunread+" "+res.data.items.length )
27+
return Promise.resolve(-1)
28+
}
29+
throw new Error('Error occured While fetching inbox data')
30+
})
31+
32+
}
33+
34+
35+

background/main.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*main back to get th*/
2+
3+
var token
4+
var totunread = 0
5+
var notiunread = 0
6+
7+
const INBOX_READ =
8+
`https://api.stackexchange.com/2.2/me/inbox/unread?key=${KEY}&site=stackoverflow&filter=!0UscT51V)r-XJhlOHzduuQu)O`
9+
10+
const NOTIFICATIONS =
11+
`https://api.stackexchange.com/2.2/me/reputation?site=stackoverflow&key=${KEY}`
12+
13+
//error noti
14+
function notifyerror(error) {
15+
browser.notifications.create({
16+
"type": "basic",
17+
"iconUrl": browser.extension.getURL("icons/addon.png"),
18+
"title": "Error",
19+
"message": `${error}`,
20+
"items": [{title:"test1",message:"ffff"}]
21+
22+
23+
})
24+
}
25+
26+
//error log function
27+
function logError(error) {
28+
console.error(`Error: ${error}`);
29+
//notifyerror(error)
30+
}
31+
32+
33+
browser.browserAction.onClicked.addListener(async function() {
34+
console.log("CLICKED")
35+
token = await getAccessToken()
36+
console.log("tok "+token)
37+
unreadnotificatons(token).then(notifynotiunread).catch(logError)
38+
unreadmessages(token).then(notifyunread).catch(logError)
39+
badge()
40+
startInterval()
41+
})
42+
43+
function startInterval(){
44+
var interval = setInterval(function(){
45+
unreadnotificatons(token)
46+
.then((val)=>{
47+
if(val!=-1){notifynotiunread}})
48+
.catch(logError)
49+
unreadmessages(token)
50+
.then((val)=>{
51+
if(val!=-1){notifyunread}})
52+
.catch(logError)
53+
badge()
54+
},60000)}
55+
56+
function badge() {
57+
if((totunread+notiunread)>0)
58+
{
59+
browser.browserAction.setBadgeText({text: (totunread+notiunread).toString()});
60+
}
61+
}
62+
63+

background/notificationInfo.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function notifynotiunread(count) {
2+
browser.notifications.create({
3+
"type": "basic",
4+
"iconUrl": browser.extension.getURL("icons/addon.png"),
5+
"title": "Reputation Changes",
6+
"message": "You have "+count+" reputation change events"
7+
8+
})
9+
}
10+
11+
12+
function unreadnotificatons(token){
13+
var url = NOTIFICATIONS+`&access_token=${token}`
14+
15+
return axios.get(url).then(res=>{
16+
17+
if(notiunread < res.data.items.length )
18+
{ console.log(notiunread+" n "+res.data.items.length )
19+
notiunread = res.data.items.length
20+
return Promise.resolve(res.data.items.length)
21+
}
22+
if(notiunread == res.data.items.length ){
23+
console.log(notiunread+" n "+res.data.items.length )
24+
return Promise.resolve(-1)
25+
}
26+
throw new Error('Error occured While fetching notification data')
27+
})
28+
}
29+

icons/License

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>Icons made by <a href="https://www.flaticon.com/authors/pixel-perfect" title="Pixel perfect">Pixel perfect</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>

manifest.json

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
{
2-
3-
"description": "Eazy access for your StackOverflow account",
4-
"manifest_version": 2,
5-
"name": "StackZilla",
6-
"version": "1.0",
7-
"homepage_url": "https://github.com/namila007/StackZilla",
8-
"icons": {
9-
"64": "icons/addon.png"
10-
},
2+
"description": "Eazy access to your StackOverflow account",
3+
"manifest_version": 2,
4+
"name": "StackZilla",
5+
"version": "1.0",
6+
"homepage_url": "https://github.com/namila007/StackZilla",
7+
"icons": {
8+
"64": "icons/addon.png",
9+
"32": "icons/toolbar.png"
10+
},
1111
"permissions": [
12-
"activeTab"
12+
"notifications",
13+
"identity",
14+
"*://api.stackexchange.com/*",
15+
"*://*.stackoverflow.com/*"
1316
],
17+
"applications": {
18+
"gecko": {
19+
20+
}
21+
},
1422
"browser_action": {
1523
"default_icon": "icons/toolbar.png",
1624
"default_title": "StackZilla",
17-
"default_popup": "popup/index.html"
25+
"browser_style": true
26+
},
27+
"background": {
28+
"scripts": [
29+
"background/authorize.js",
30+
"background/main.js",
31+
"background/inboxInfo.js",
32+
"background/notificationInfo.js",
33+
"background/axios.js"
34+
35+
]
1836
},
19-
37+
2038
"content_scripts": [
2139
{
22-
"matches": ["<all_urls>"],
23-
"js": ["stackzilla.js"]
40+
"matches": ["*://*/*"],
41+
"js": ["popup/stackpopup.js"]
2442
}
25-
]
43+
],
44+
"options_ui": {
45+
"page": "options/options.html"
46+
},
47+
"author": "Namila Bandara"
48+
2649

2750
}

options/options.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
<div>Redirect URL: <span id="redirect-url"></span></div>
11+
12+
<script src="options.js"></script>
13+
14+
</body>
15+
16+
</html>

options/options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
Display the redirect URL.
3+
*/
4+
document.querySelector("#redirect-url").textContent = browser.identity.getRedirectURL()

popup/blank.html

Whitespace-only changes.

0 commit comments

Comments
 (0)