Skip to content

Commit 014e56f

Browse files
committed
working 😆 no popup
1 parent 86b7c53 commit 014e56f

File tree

7 files changed

+110
-77
lines changed

7 files changed

+110
-77
lines changed

background/all.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

background/authorize.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/* exported getAccessToken */
22

3+
const REDIRECT_URL = browser.identity.getRedirectURL();
4+
const CLIENT_ID = "12428";
35
const KEY = "f26RUH3uoCiokrEYNeDf9Q(("
6+
const SCOPES = ["read_inbox"];
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+
10+
411

512
const VALIDATION_BASE_URL="https://api.stackexchange.com/2.2/";
613

@@ -44,3 +51,13 @@ function validate(redirectURL) {
4451
}
4552

4653

54+
async function authorize() {
55+
return await browser.identity.launchWebAuthFlow({
56+
interactive: true,
57+
url: AUTH_URL
58+
});
59+
}
60+
61+
function getAccessToken() {
62+
return authorize().then(validate);
63+
}

background/inboxInfo.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 new Promise((resolve,reject)=>{
17+
get(url, (msg1)=>{
18+
if(msg1.error_message){ resolve("Error: "+msg1.error_message)}
19+
console.log(msg1)
20+
let msg = JSON.parse(msg1)
21+
console.log(msg.items.length)
22+
if(totunread < msg.items.length ){
23+
notifyunread(msg.items.length)
24+
totunread = msg.items.length
25+
resolve(msg.items.length)
26+
}
27+
})
28+
})
29+
}
30+
31+
32+

background/main.js

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,41 @@
11
/*main back to get th*/
22

33
var token
4+
var totunread = -1
5+
var notiunread = -1
46

57
const INBOX_READ =
6-
`https://api.stackexchange.com/2.2/me/inbox?page=1&pagesize=5&key=${KEY}&site=stackoverflow&filter=!LURAJLCc5n-3UL-M6AmXYq`
8+
`https://api.stackexchange.com/2.2/me/inbox/unread?page=1&pagesize=5&key=${KEY}&site=stackoverflow&filter=!0UscT51V)r-XK9Kg9bV54nqom`
9+
10+
const NOTIFICATIONS =
11+
`https://api.stackexchange.com/2.2/me/notifications/unread?page=1&pagesize=2&site=stackoverflow&filter=!0UscT5320P8DSPTPhCF)1tNYL&key=${KEY}`
712

813
//error log function
914
function logError(error) {
1015
console.error(`Error: ${error}`);
1116
}
1217

13-
browser.runtime.onMessage.addListener(recivemessage)
14-
15-
function recivemessage (message,sender,sendResponse){
16-
if(message.command === "tokenurl"){
17-
console.log(message.data)
18-
token = validate(message.data).catch(logError).then((val)=>{
19-
console.log("val "+val)
20-
token = val;
21-
sendResponse({response: val})
22-
23-
}).then(()=>{
24-
browser.notifications.create({
25-
"type": "basic",
26-
"iconUrl": browser.extension.getURL("icons/addon.png"),
27-
"title": "Token Created",
28-
"message": "Token is created"
29-
})
30-
},logError)
31-
return true
32-
}
33-
if(message.command == "inbox") {
34-
var url = INBOX_READ+`&access_token=${message.token}`
35-
get(url, (msg1)=>{
36-
//console.log(msg1)
37-
38-
sendResponse({response: msg1})
39-
})
40-
41-
}
42-
43-
44-
return true
45-
46-
}
47-
48-
function get(url, callback){
49-
var xmlHttp = new XMLHttpRequest();
50-
xmlHttp.onreadystatechange = function() {
51-
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
52-
callback(xmlHttp.responseText);
53-
}
54-
xmlHttp.open("GET", url, true); // true for asynchronous
55-
xmlHttp.send(null);
18+
function get(url, callback){
19+
var xmlHttp = new XMLHttpRequest();
20+
xmlHttp.onreadystatechange = function() {
21+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
22+
callback(xmlHttp.responseText);
5623
}
57-
58-
// return true
59-
24+
xmlHttp.open("GET", url, true); // true for asynchronous
25+
xmlHttp.send(null);
26+
}
27+
28+
browser.browserAction.onClicked.addListener(async function() {
29+
console.log("CLICKED")
30+
token = await getAccessToken()
31+
console.log("tok "+token)
32+
unreadnotificatons(token).catch(logError)
33+
unreadmessages(token).then((data)=>{console.log(data)}).catch(logError)
34+
35+
})
36+
37+
38+
6039

6140

6241

background/notificationInfo.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function notifynotiunread(count) {
2+
browser.notifications.create({
3+
"type": "basic",
4+
"iconUrl": browser.extension.getURL("icons/addon.png"),
5+
"title": "Notifications",
6+
"message": "You have "+count+" unread Notifications"
7+
})
8+
}
9+
10+
11+
function unreadnotificatons(token){
12+
var url = NOTIFICATIONS+`&access_token=${token}`
13+
console.log(url)
14+
15+
return new Promise((resolve,reject)=>{
16+
get(url, (msg1)=>{
17+
console.log(msg1)
18+
let msg = JSON.parse(msg1)
19+
console.log(msg.items.length)
20+
if(notiunread < msg.items.length ){
21+
notifynotiunread(msg.items.length)
22+
notiunread = msg.items.length
23+
resolve(msg.items.length)
24+
}
25+
26+
})
27+
})
28+
29+
}
30+

manifest.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
],
1717
"applications": {
1818
"gecko": {
19-
"id": "stackzilla2@mozilla.org"
19+
"id": "stackzilla5@mozilla.org"
2020
}
2121
},
2222
"browser_action": {
2323
"default_icon": "icons/toolbar.png",
2424
"default_title": "StackZilla",
25-
"default_popup": "popup/index.html"
25+
"browser_style": true
2626
},
2727
"background": {
2828
"scripts": [
2929
"background/authorize.js",
30-
"background/main.js"
30+
"background/main.js",
31+
"background/inboxInfo.js",
32+
"background/notificationInfo.js"
3133
]
3234
},
33-
3435
"content_scripts": [
3536
{
3637
"matches": ["*://*/*"],

popup/userinfo.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)