From d09e94f4504c0ed6621e3885c79398067069670c Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 14 Sep 2017 11:27:43 +0200 Subject: [PATCH] Storage backend refactored --- app/background.js | 2 - app/content_scripts.js | 1 - js/generic_btn_actions.js | 1 - js/login.js | 1 - js/submit_article.js | 133 +++++++++++++++++++++----------------- js/subscribe_to_feed.js | 80 ++++++++++++----------- manifest.json | 2 +- 7 files changed, 115 insertions(+), 105 deletions(-) diff --git a/app/background.js b/app/background.js index 1140663..3e8eb6a 100755 --- a/app/background.js +++ b/app/background.js @@ -54,9 +54,7 @@ function checkRSSFeeds() { } ); - }); - } //listen for new tab to be activated diff --git a/app/content_scripts.js b/app/content_scripts.js index df55f2e..ae75700 100755 --- a/app/content_scripts.js +++ b/app/content_scripts.js @@ -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"; } diff --git a/js/generic_btn_actions.js b/js/generic_btn_actions.js index eeed79d..f5396e6 100644 --- a/js/generic_btn_actions.js +++ b/js/generic_btn_actions.js @@ -10,7 +10,6 @@ $( document ).ready(function() { }); $('#disconnect-btn').on('click', function(){ - localStorage.removeItem('loginToken'); chrome.storage.local.remove('loginToken'); window.location = "login.html"; }); diff --git a/js/login.js b/js/login.js index 830b75e..9173a28 100755 --- a/js/login.js +++ b/js/login.js @@ -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"; } ); diff --git a/js/submit_article.js b/js/submit_article.js index d1ff408..f1d2ef4 100644 --- a/js/submit_article.js +++ b/js/submit_article.js @@ -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; @@ -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; }, @@ -97,8 +109,7 @@ $( document ).ready(function() { name: tag }; }); - }, - cache: false //NEW! + } } }); @@ -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) { diff --git a/js/subscribe_to_feed.js b/js/subscribe_to_feed.js index ccf9071..243f292 100644 --- a/js/subscribe_to_feed.js +++ b/js/subscribe_to_feed.js @@ -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"; } @@ -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" )){ @@ -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(); @@ -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"); }, diff --git a/manifest.json b/manifest.json index 3c07413..a423fb9 100755 --- a/manifest.json +++ b/manifest.json @@ -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": {