Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions background/all.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions background/authorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* exported getAccessToken */

const KEY = "f26RUH3uoCiokrEYNeDf9Q(("

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

function extractAccessToken(redirectUri) {
let m = redirectUri.match(/[#?](.*)/);
if (!m || m.length < 1)
return null;
let params = new URLSearchParams(m[1].split("#")[0]);
return params.get("access_token");
}

function validate(redirectURL) {
const accessToken = extractAccessToken(redirectURL);
console.log(accessToken + " access")
if (!accessToken) {
throw "Authorization failure";
}
const validationURL = `${VALIDATION_BASE_URL}access-tokens/${accessToken}?key=${KEY}`;

console.log(validationURL)
const validationRequest = new Request(validationURL, {
method: "GET"
});

function checkResponse(response) {
return new Promise((resolve, reject) => {
if (response.status != 200) {
reject("Token validation error");
}
response.json().then((json) => {
if (json.items) {
resolve(accessToken);
} else {
reject("Token validation error");
}
});
});
}

return fetch(validationRequest).then(checkResponse);
}


62 changes: 62 additions & 0 deletions background/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*main back to get th*/

var token

const INBOX_READ =
`https://api.stackexchange.com/2.2/me/inbox?page=1&pagesize=5&key=${KEY}&site=stackoverflow&filter=!LURAJLCc5n-3UL-M6AmXYq`

//error log function
function logError(error) {
console.error(`Error: ${error}`);
}

browser.runtime.onMessage.addListener(recivemessage)

function recivemessage (message,sender,sendResponse){
if(message.command === "tokenurl"){
console.log(message.data)
token = validate(message.data).catch(logError).then((val)=>{
console.log("val "+val)
token = val;
sendResponse({response: val})

}).then(()=>{
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/addon.png"),
"title": "Token Created",
"message": "Token is created"
})
},logError)
return true
}
if(message.command == "inbox") {
var url = INBOX_READ+`&access_token=${message.token}`
get(url, (msg1)=>{
//console.log(msg1)

sendResponse({response: msg1})
})

}


return true

}

function get(url, callback){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
}

// return true




12 changes: 12 additions & 0 deletions content_script/stackzilla.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function() {
/**
* Check and set a global guard variable.
* If this content script is injected into the same page again,
* it will do nothing next time.
*/
if (window.hasRun) {
return;
}
window.hasRun = true;

})
1 change: 1 addition & 0 deletions icons/License
Original file line number Diff line number Diff line change
@@ -0,0 +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>
45 changes: 32 additions & 13 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
{

"description": "Eazy access for your StackOverflow account",
"manifest_version": 2,
"name": "StackZilla",
"version": "1.0",
"homepage_url": "https://github.com/namila007/StackZilla",
"icons": {
"64": "icons/addon.png"
},
"description": "Eazy access to your StackOverflow account",
"manifest_version": 2,
"name": "StackZilla",
"version": "1.0",
"homepage_url": "https://github.com/namila007/StackZilla",
"icons": {
"64": "icons/addon.png",
"32": "icons/toolbar.png"
},
"permissions": [
"activeTab"
"notifications",
"identity",
"*://api.stackexchange.com/*",
"*://*.stackoverflow.com/*"
],
"applications": {
"gecko": {
"id": "stackzilla2@mozilla.org"
}
},
"browser_action": {
"default_icon": "icons/toolbar.png",
"default_title": "StackZilla",
"default_popup": "popup/index.html"
},
"background": {
"scripts": [
"background/authorize.js",
"background/main.js"
]
},

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["stackzilla.js"]
"matches": ["*://*/*"],
"js": ["popup/stackpopup.js"]
}
]
],
"options_ui": {
"page": "options/options.html"
},
"author": "Namila Bandara"


}
16 changes: 16 additions & 0 deletions options/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
</head>

<body>

<div>Redirect URL: <span id="redirect-url"></span></div>

<script src="options.js"></script>

</body>

</html>
4 changes: 4 additions & 0 deletions options/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
Display the redirect URL.
*/
document.querySelector("#redirect-url").textContent = browser.identity.getRedirectURL()
Empty file added popup/blank.html
Empty file.
7 changes: 7 additions & 0 deletions popup/css/bootstrap.min.css

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions popup/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
html, body {
width: 600px;
height: 600px;

}
.button {
margin: 3% auto;
padding: 4px;
text-align: center;
font-size: 1.5em;
cursor: pointer;
}

42 changes: 40 additions & 2 deletions popup/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
<html >
Hello
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css"/>
</head>

<body>
<div class="container-fluid">
<div id="popup-content row" class="row">
<div class="col-sm col-md">
<button type="button" class="btn btn-info small" id="tokenbtn">Get Token</button>
</div>
<div class="token" id="token"></div>
<div class="col user">
</div>
</div>

<div class="row" id="main_inbox" >
<div class="col-12">
<button type="button" class="btn btn-primary" id="showinboxbtn">
Inbox <span class="badge badge-light" id="unreadinboxcount"></span>
<span class="sr-only">Unread Messages</span>
</button>
</div>
<div class="col-12">
<ul id="inbox" style="visibility: hidden;"></ul>
</div>

</div>

</div>


<script type='text/javascript' src="scripts/jquery-3.3.1.min.js"></script>
<script src="stackpopup.js"></script>
</body>


</html>
2 changes: 2 additions & 0 deletions popup/scripts/jquery-3.3.1.min.js

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions popup/stackpopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

//initializing

const REDIRECT_URL = browser.identity.getRedirectURL();
const CLIENT_ID = "12428";
const KEY = "f26RUH3uoCiokrEYNeDf9Q(("
const SCOPES = ["read_inbox"];
const AUTH_URL =
`https://stackoverflow.com/oauth/dialog?client_id=${CLIENT_ID}&key=${KEY}&redirect_uri=${REDIRECT_URL}&scope=${encodeURIComponent(SCOPES.join(' '))}`;


function handleResponse(message) {
//console.log(`background script sent a response: ${message.response}`)
getinbox(message.response)

}

function handleError(error) {
console.log(`Error: ${error}`);
}


document.getElementById('tokenbtn').addEventListener("click", async function getToken () {

var tokenurl = await browser.identity.launchWebAuthFlow({
interactive: true,
url: AUTH_URL
})
var token = browser.runtime.sendMessage({
command: "tokenurl" ,
data: tokenurl })
token.then(handleResponse,handleError)


})

document.getElementById('showinboxbtn').addEventListener("click",()=>{
console.log("clicked")
var inbox = document.getElementById("inbox")
if(inbox.style.visibility == 'hidden') {
inbox.style.visibility = 'visible'
}else {
inbox.style.visibility = 'hidden'
}
})

function handleinbox(message) {
console.log("Inbox")

let msg = JSON.parse(message.response)


var inbox = document.getElementById("inbox")

var unreadcount = 0
for (var i=0; i<msg.items.length; i++){
if(msg.items[i].is_unread === true) unreadcount++
var item = document.createElement('li')
item.innerHTML =
'<a href="'+ msg.items[i].link +'">' + msg.items[i].title + '</a>'
inbox.appendChild(item)
}
var unread = document.getElementById("unreadinboxcount")
unread.innerHTML = unreadcount
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/addon.png"),
"title": "Inbox",
"message": "You have "+unreadcount+" unread messages"
})


}

function getinbox(token) {
var inbox = browser.runtime.sendMessage({
command: "inbox",
token: token
})
inbox.then(handleinbox,handleError)
}



25 changes: 25 additions & 0 deletions popup/userinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
Fetch the user's info, passing in the access token in the Authorization
HTTP request header.
*/

/* exported getUserInfo */

function getUserInfo(accessToken) {
const requestURL = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
const requestHeaders = new Headers();
requestHeaders.append('Authorization', 'Bearer ' + accessToken);
const driveRequest = new Request(requestURL, {
method: "GET",
headers: requestHeaders
});

return fetch(driveRequest).then((response) => {
if (response.status === 200) {
return response.json();
} else {
throw response.status;
}
});

}