Skip to content

Commit

Permalink
Storage backend refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
DEKHTIARJonathan committed Sep 14, 2017
1 parent ef1cd29 commit d09e94f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 105 deletions.
2 changes: 0 additions & 2 deletions app/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ function checkRSSFeeds() {

}
);

});

}

//listen for new tab to be activated
Expand Down
1 change: 0 additions & 1 deletion app/content_scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (!(result.loginToken))
target_webpage += "login.html";
else{
localStorage.setItem("loginToken", result.loginToken);
target_webpage += "submit_article.html";
}

Expand Down
1 change: 0 additions & 1 deletion js/generic_btn_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ $( document ).ready(function() {
});

$('#disconnect-btn').on('click', function(){
localStorage.removeItem('loginToken');
chrome.storage.local.remove('loginToken');
window.location = "login.html";
});
Expand Down
1 change: 0 additions & 1 deletion js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ $( document ).ready(function() {
chrome.storage.local.set({
'loginToken': response.token
}, function() {
localStorage.setItem("loginToken", response.token);
window.location = "submit_article.html";
}
);
Expand Down
133 changes: 72 additions & 61 deletions js/submit_article.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
$( document ).ready(function() {

var endpoint = null;
var title = null;
var page_url = null;
var endpoint = null;
var auth_token = null;
var title = null;
var page_url = null;

var max_tags = 5;
var max_suggestion_display = 5;
Expand All @@ -16,78 +17,89 @@ $( document ).ready(function() {
'autoformat'
];

chrome.runtime.sendMessage({action: "get_endpoint"}, function(response) {
if (response.result == "success"){
endpoint = response.endpoint;
try{
chrome.runtime.sendMessage({action: "get_endpoint"}, function(response) {
if (response.result == "success"){
endpoint = response.endpoint;

chrome.runtime.sendMessage({action: "get_pageinfo"}, function(response) {
if (response.result == "success"){
chrome.storage.local.get('loginToken', function(result) {
if (result.loginToken){
auth_token = result.loginToken;

if (response.rss_link != null){
$("#subscribe-btn").toggleClass('disabled', false);
}
else {
$("#subscribe-btn").toggleClass('disabled', true);
}
chrome.runtime.sendMessage({action: "get_pageinfo"}, function(response) {
if (response.result == "success"){

$("#title").data("init", response.title);
$("#link").data("init", response.url);

$.ajax({
url: endpoint + "api/1.0/authenticated/get/user/preferences/",
type: "GET",
dataType: "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + localStorage.getItem("loginToken"));
},
success: function(data) {
if (data.success) {
for (switch_box in switches_list) {
var input = $("#" + switches_list[switch_box]);

pref_value = data.preferences[switches_list[switch_box]];

if (pref_value == "disabled")
input.prop("disabled", true);
else
input.data("init", pref_value);
}

init_webpage();

} else
console.log("Error: " + data.error);
}
});
}
else{
console.log("Response:" + response.result);
}
});
}
else{
console.log("Response:" + response.result);
}
});
if (response.rss_link != null){
$("#subscribe-btn").toggleClass('disabled', false);
}
else {
$("#subscribe-btn").toggleClass('disabled', true);
}

$("#title").data("init", response.title);
$("#link").data("init", response.url);

$.ajax({
url: endpoint + "api/1.0/authenticated/get/user/preferences/",
type: "GET",
dataType: "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + auth_token);
},
success: function(data) {
if (data.success) {
for (switch_box in switches_list) {
var input = $("#" + switches_list[switch_box]);

pref_value = data.preferences[switches_list[switch_box]];

if (pref_value == "disabled")
input.prop("disabled", true);
else
input.data("init", pref_value);
}

init_webpage();

} else{
throw ("Impossible to obtain user preferences:" + data.error);
}
}
});
}
else{
throw ("Impossible to obtain pageinfos:" + response.result);
}
});
}
else{
throw ("User Not Logged In: loginToken does not exists!");
}
});
}
else {
throw ("Impossible to obtain the API endpoint.");
}
});
} catch (err) {
console.log("Error: " + err);
window.location = "login.html";
}

function init_webpage(){
try {

if (localStorage.getItem("loginToken") === null) {
throw ("User Not Logged In: loginToken does not exists!");
}

var tags = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: max_suggestion_display,
prefetch: {
url: endpoint + "api/1.0/authenticated/get/tags/",
cache: false,
prepare: function(settings) {
settings.type = "GET";
settings.beforeSend = function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + localStorage.getItem("loginToken"));
xhr.setRequestHeader("Authorization", 'Token ' + auth_token);
};
return settings;
},
Expand All @@ -97,8 +109,7 @@ $( document ).ready(function() {
name: tag
};
});
},
cache: false //NEW!
}
}
});

Expand Down Expand Up @@ -189,7 +200,7 @@ $( document ).ready(function() {
autoformat: $('#autoformat').prop('checked'),
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + localStorage.getItem("loginToken"));
xhr.setRequestHeader("Authorization", 'Token ' + auth_token);
},
success: function(data) {
if (data.success) {
Expand Down
80 changes: 42 additions & 38 deletions js/subscribe_to_feed.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
$( document ).ready(function() {

var endpoint = null;

try {
if (localStorage.getItem("loginToken") === null) {
throw ("User Not Logged In: loginToken does not exists!");
}
}
catch (err) {
var endpoint = null;
var auth_token = null;

try{
chrome.runtime.sendMessage({action: "get_endpoint"}, function(response) {
if (response.result == "success"){
endpoint = response.endpoint;

chrome.storage.local.get('loginToken', function(result) {
if (result.loginToken){
auth_token = result.loginToken;

chrome.runtime.sendMessage({action: "get_pageinfo"}, function(response) {
if (response.result == "success"){
if (response.rss_link == null){
window.location = "submit_article.html";
}

$("#rssfeed_link").data("init", response.rss_link);
$("#rssfeed_link").val(response.rss_link);
format_labels();

updateFeedTitle();

}
else{
throw ("Impossible to obtain pageinfos:" + response.result);
}
});
}
else{
throw ("User Not Logged In: loginToken does not exists!");
}
});
}
else {
throw ("Impossible to obtain the API endpoint.");
}
});
} catch (err) {
console.log("Error: " + err);
window.location = "login.html";
}
Expand Down Expand Up @@ -64,7 +96,7 @@ $( document ).ready(function() {
timeout: 5000, // sets timeout to 5 seconds
dataType : "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + localStorage.getItem("loginToken"));
xhr.setRequestHeader("Authorization", 'Token ' + auth_token);
info_div.text("Validating your input ...");
info_div.attr("class", "green-text text-darken-2");
if (title_label.hasClass( "active" )){
Expand Down Expand Up @@ -112,34 +144,6 @@ $( document ).ready(function() {
});
}

chrome.runtime.sendMessage({action: "get_endpoint"}, function(response) {
if (response.result == "success"){
endpoint = response.endpoint;

chrome.runtime.sendMessage({action: "get_pageinfo"}, function(response) {
if (response.result == "success"){

if (response.rss_link == null){
window.location = "submit_article.html";
}

$("#rssfeed_link").data("init", response.rss_link);
$("#rssfeed_link").val(response.rss_link);
format_labels();

updateFeedTitle();

}
else{
console.log("Response:" + response.result);
}
});
}
else{
console.log("Response:" + response.result);
}
});

$('#rssfeed_link').on('paste', function (){
setTimeout($.proxy(function () {
$(this).blur();
Expand Down Expand Up @@ -194,7 +198,7 @@ $( document ).ready(function() {
timeout: 8000, // sets timeout to 8 seconds
dataType : "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", 'Token ' + localStorage.getItem("loginToken"));
xhr.setRequestHeader("Authorization", 'Token ' + auth_token);
info_div.text("Verifying and subscribing to the RSS Feed ...");
info_div.attr("class", "green-text text-darken-2");
},
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"short_name": "Feedcrunch",
"author": "Jonathan DEKHTIAR",
"manifest_version": 2,
"version": "1.0.7",
"version": "1.0.8",

"description": "Feedcrunch.IO - Chrome Extension",
"icons": {
Expand Down

0 comments on commit d09e94f

Please sign in to comment.