diff --git a/background.html b/background.html index 931d41b..26e80ad 100644 --- a/background.html +++ b/background.html @@ -1,11 +1,11 @@ - Envato sales desktop notification - - + Envato sales desktop notification + + - + diff --git a/background.js b/background.js index 829799a..7f85c04 100644 --- a/background.js +++ b/background.js @@ -1,37 +1,55 @@ // Show the config file on installation chrome.runtime.onInstalled.addListener(function(object) { - if (localStorage.firsttime != 'false' || localStorage.firsttime == 'undefined' || !localStorage.firsttime ) { - var optionsurl = chrome.extension.getURL("options.html"); - chrome.tabs.create({ - url: optionsurl - }, function(tab) {}); - - localStorage.firsttime = false; -} + get_option('firsttime', function(value){ + if(value != 'false' || !value) { + var optionsurl = chrome.extension.getURL("options.html"); + chrome.tabs.create({ + url: optionsurl + }, function(tab) {}); + + save_option('firsttime', 'false'); + } + }); -var currentversion = chrome.app.getDetails().version; + var currentversion = chrome.app.getDetails().version; - if (!localStorage.version || localStorage.version != currentversion) { - chrome.browserAction.setBadgeText({text:"NEW"}); - localStorage.version = currentversion -} + get_option('version', function(value){ + if(!value || value != currentversion) { + chrome.browserAction.setBadgeText({text:"NEW"}); + + save_option('version', currentversion); + } + }); //chrome.browserAction.setBadgeBackgroundColor({color:[255, 64, 64, 230]}); - - -}); + // Check if localStorage is in usage + if(!isEmpty(localStorage)) { + // localStorage is in use, so move all options from there to chrome.storage + //delete localStorage.apikey; + + for(var i = 0; i < localStorage.length; i++) { + var name = localStorage.key(i); + var value = localStorage.getItem(localStorage.key(i)); + + save_option(name, value); + + delete localStorage[name]; + } + } + +}); // Get Local Storage value in Content Script /*chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { - if (request.method == "getLocalStorage") - sendResponse({data: localStorage[request.key]}); - else - sendResponse({}); // snub them. -}); -*/ + if (request.method == "getLocalStorage") + sendResponse({data: localStorage[request.key]}); + else + sendResponse({}); // snub them. + }); + */ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { switch (message.method) { @@ -51,19 +69,26 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { }); } break; - // ... + // ... } }); -// Open Opeions Page if clicked on icons +// Open Options Page if clicked on icons chrome.browserAction.onClicked.addListener(function(tab) { //Fired when User Clicks ICON chrome.tabs.create({ url: "options.html" }); - chrome.browserAction.setBadgeText({text:""}); + chrome.browserAction.setBadgeText({text:""}); }); +function isEmpty(obj) { + for(var prop in obj) { + if(obj.hasOwnProperty(prop)) + return false; + } + return true; +} function updatedSettings() { //chrome.runtime.reload(); @@ -74,22 +99,22 @@ function updatedSettings() { // Test for notification support. if (window.Notification) { - if (localStorage.sales_notification != 'false') { - // While activated, show notifications at the display frequency. - sales_notification(); - setInterval(function() { + get_option('sales_notification', function(value){ + if(value != 'false') { sales_notification(); - }, 60000); - } - - if (localStorage.comment_notification != 'false') { - // While activated, show notifications at the display frequency. - comment_notification(); - setInterval(function() { + setInterval(function() { + sales_notification(); + }, 60000); + } + }); + get_option('comment_notification', function(value){ + if(value != 'false') { comment_notification(); - }, 60000); - } - + setInterval(function() { + comment_notification(); + }, 60000); + } + }); } @@ -97,56 +122,59 @@ if (window.Notification) { // Sales Notifications function sales_notification() { + get_option('username', function(value){ + var username = value; + if(typeof username == 'undefined') { + return false; + } + get_option('apikey', function(value){ + var apikey = value; + if(typeof apikey == 'undefined') { + return false; + } + get_option('earnings', function(value){ + var earnings = value; - var username = localStorage.username; - var apikey = localStorage.apikey; - var earnings = localStorage.earnings; - - if (typeof username == 'undefined' || typeof apikey == 'undefined') { - return false; - } + // Use Envato API to check sales + $.get('http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json', function(data) { - // Use Envato API to check sales - $.get('http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json', function(data) { + //get current sales + var new_earnings = data.account.available_earnings; + var first_name = data.account.firstname; - //get current sales - var new_earnings = data.account.available_earnings; - var first_name = data.account.firstname; + if (earnings < new_earnings) { - //earnings < new_earnings - // testing: new_earnings > 1 + $.get('http://marketplace.envato.com/api/v3/' + username + '/' + apikey + '/recent-sales.json', function(salesdata) { - if (earnings < new_earnings) { + //get current sales + var sold_item = salesdata["recent-sales"][0].item; + var item_price = salesdata["recent-sales"][0].amount; + show_notification(new_earnings, item_price, first_name, sold_item, 'item'); + play_sound(); - $.get('http://marketplace.envato.com/api/v3/' + username + '/' + apikey + '/recent-sales.json', function(salesdata) { + }); + } - //get current sales - var sold_item = salesdata["recent-sales"][0].item; - var item_price = salesdata["recent-sales"][0].amount; - show_notification(new_earnings, item_price, first_name, sold_item, 'item'); - play_sound(); + save_option('earnings', new_earnings); + }); }); - } - - - localStorage.earnings = new_earnings; - + }); }); - } // Play a sound on new sale function play_sound() { - if (localStorage.play_sound != 'false') { - if ($('#cha-ching').length) $('#cha-ching').remove(); - $('').appendTo('body'); - } - return false; + get_option('play_sound', function(value){ + if(value != 'false') { + if ($('#cha-ching').length) $('#cha-ching').remove(); + $('').appendTo('body'); + } + return false; + }); } function show_notification(new_earnings, item_price, first_name, sold_item, item) { - var praiseArray = ['Woohoo', 'Bravo', 'Wow', 'Ahoy', 'Yay', 'Yikes', 'Hooray', 'Whoa', 'Woot', 'Oh joy', first_name]; var randomPraise = praiseArray[Math.floor(Math.random() * praiseArray.length)]; @@ -158,69 +186,103 @@ function show_notification(new_earnings, item_price, first_name, sold_item, item notification.onclick = function() { window.open("http://themeforest.net/statement"); } - if (localStorage.auto_hide_sales_notification != 'false') { - setTimeout(function() { - notification.close() - }, 15000); - } + get_option('auto_hide_sales_notification', function(value){ + if(value != 'false') { + setTimeout(function() { + notification.close() + }, 15000); + } + }); } function comment_notification() { + get_option('username', function(value){ + var username = value; + get_option('new_comment_id', function(value){ + var last_comment_id = value; + + $.get('http://themeforest.net/feeds/user_item_comments/' + username + '.atom', function(data) { + //console.log(data); + var comment_feed = $.xml2json(data); + //console.log(comment_feed); + var comment_id = comment_feed.entry[0].id; + var comment_id_hash = comment_id.substr(comment_id.lastIndexOf('/') + 1); + var comment_author = comment_feed.entry[0].author.name; + var new_comment = /* $(comment_feed.entry[0].content.text).text(); */ comment_feed.entry[0].content.text.replace(/(<([^>]+)>)/ig, ""); + new_comment = new_comment.replace(/\s{2,}/g, ' '); + var new_comment_item = $.trim(comment_feed.entry[0].title).substring(0, 20).split(" ").slice(0, -1).join(" ") + "..."; + var new_comment_url = comment_feed.entry[0].link.href; + //console.log(new_comment); + + if (last_comment_id != comment_id && comment_author != username) { + show_comments(new_comment, new_comment_item, new_comment_url, comment_id_hash); + save_option('new_comment_id', comment_id); + play_notification(); + } - var username = localStorage.username; - var last_comment_id = localStorage.new_comment_id; - - $.get('http://themeforest.net/feeds/user_item_comments/' + username + '.atom', function(data) { - //console.log(data); - var comment_feed = $.xml2json(data); - //console.log(comment_feed); - var comment_id = comment_feed.entry[0].id; - var comment_id_hash = comment_id.substr(comment_id.lastIndexOf('/') + 1); - var comment_author = comment_feed.entry[0].author.name; - var new_comment = /* $(comment_feed.entry[0].content.text).text(); */ comment_feed.entry[0].content.text.replace(/(<([^>]+)>)/ig, ""); - new_comment = new_comment.replace(/\s{2,}/g, ' '); - var new_comment_item = $.trim(comment_feed.entry[0].title).substring(0, 20).split(" ").slice(0, -1).join(" ") + "..."; - var new_comment_url = comment_feed.entry[0].link.href; - //console.log(new_comment); - - if (last_comment_id != comment_id && comment_author != username) { - show_comments(new_comment, new_comment_item, new_comment_url, comment_id_hash); - localStorage.new_comment_id = comment_id; - play_notification(); - } + }); - }); + // Play a sound on new comment + function play_notification() { + get_option('comment_sound', function(value){ + if(value != 'false') { + if ($('#comment_sound').length) $('#comment_sound').remove(); + $('').appendTo('body'); + } + return false; + }); + } - // Play a sound on new comment - function play_notification() { - if (localStorage.comment_sound != 'false') { - if ($('#comment_sound').length) $('#comment_sound').remove(); - $('').appendTo('body'); - } - return false; - } + function show_comments(new_comment, new_comment_item, new_comment_url, comment_id_hash) { + + var c_notification = new Notification('New Comment for ' + new_comment_item, { + icon: 'img/48.png', + body: new_comment + }); + c_notification.onclick = function() { + window.open(new_comment_url + '/' + comment_id_hash); + } + get_option('auto_hide_comment_notification', function(value){ + if(value != 'false') { + setTimeout(function() { + c_notification.close() + }, 15000); + } + }); - function show_comments(new_comment, new_comment_item, new_comment_url, comment_id_hash) { + } - var c_notification = new Notification('New Comment for ' + new_comment_item, { - icon: 'img/48.png', - body: new_comment }); + }); +} - c_notification.onclick = function() { - window.open(new_comment_url + '/' + comment_id_hash); +/** + * Saves option to Chrome.storage + */ +function save_option(name, value){ + var object = {}; + object[name] = value; + + chrome.storage.sync.set(object, function() { + if(chrome.extension.lastError) { + console.log('An error occured: ' + chrome.extension.lastError.message); + alert('An error occured: ' + chrome.extension.lastError.message); + return false; + } else { + return true; } - - if (localStorage.auto_hide_comment_notification != 'false') { - setTimeout(function() { - c_notification.close() - }, 15000); - } - - } + }); +} +/** + * Gets option from Chrome.storage + */ +function get_option(name, callback) { + chrome.storage.sync.get(name, function(response) { + callback(response[name], name); + }) } \ No newline at end of file diff --git a/better-envato.js b/better-envato.js index b21927f..443861d 100644 --- a/better-envato.js +++ b/better-envato.js @@ -1,407 +1,558 @@ /* -Name: Better Envato -Keywords: make Envato Better -Created by: Surjith S M © 2015-2020 -*/ -var username, apikey, openexchange, currency, localise_earnings, localise_earnings_table, localise_earnings_page, hide_statement, verify_purchase, create_hrefs; - -chrome.runtime.sendMessage({ - method: "getLocalStorage", - keys: ["username", "apikey", "openexchange", "currency", "localise_earnings", 'localise_earnings_table', 'localise_earnings_page', 'hide_statement', 'verify_purchase', 'create_hrefs', 'hide_earnings' ] - }, - function(response) { - username = response.data.username; - apikey = response.data.apikey; - openexchange = response.data.openexchange; - currency = response.data.currency; - localise_earnings = response.data.localise_earnings; - localise_earnings_table = response.data.localise_earnings_table; - localise_earnings_page = response.data.localise_earnings_page; - hide_statement = response.data.hide_statement; - verify_purchase = response.data.verify_purchase; - create_hrefs = response.data.create_hrefs; - hide_earnings = response.data.hide_earnings; - } -); - -$(document).ready(function() { - if (localise_earnings != 'false') { - if (username == 'undefined' || apikey == 'undefined' || openexchange == 'undefined') { + Name: Better Envato + Keywords: make Envato Better + Created by: Surjith S M © 2015-2020 + */ +var username, apikey, openexchange, currency, localise_earnings, localise_earnings_table, localise_earnings_page, hide_statement, verify_purchase, create_hrefs, cache_currency_rate, currency_rate; + +/** + * Saves option to Chrome.storage + */ +function save_option(name, value){ + var object = {}; + object[name] = value; + + chrome.storage.sync.set(object, function() { + if(chrome.extension.lastError) { + console.log('An error occured: ' + chrome.extension.lastError.message); return false; } else { - dollartToInr(); + return true; } - } + }); +} -}); +// Load options from Chrome's storage +chrome.storage.sync.get(null, function(response){ + username = response.username; + apikey = response.apikey; + openexchange = response.openexchange; + currency = response.currency; + localise_earnings = response.localise_earnings; + localise_earnings_table = response.localise_earnings_table; + localise_earnings_page = response.localise_earnings_page; + hide_statement = response.hide_statement; + verify_purchase = response.verify_purchase; + create_hrefs = response.create_hrefs; + hide_earnings = response.hide_earnings; + cache_currency_rate = response.cache_currency_rate; + currency_rate = response.currency_rate; + + $(document).ready(function() { + if (localise_earnings != 'false') { + if (username == 'undefined' || apikey == 'undefined' || openexchange == 'undefined') { + return false; + } else { + dollartToInr(); + } + } + if(cache_currency_rate != 'false') { + var exchange_rate = currency_rate.split('||') || 1; + + var last_cached_time = exchange_rate[0]; + var last_exchange_rate = exchange_rate[1]; + var current_timestamp = Math.floor(Date.now() / 1000); + var day_ago_time = current_timestamp - 86400; + + if(exchange_rate[2] != currency || last_cached_time < day_ago_time) { + // Fetch latest currency rate + var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange; + $.getJSON(conversionurl, function(data) { + save_option('currency_rate', current_timestamp+'||'+data.rates[currency]+'||'+currency); + }); + } + } + }); //function for converting string into indian currency format -function inr_currency(nStr) { - nStr += ''; - x = nStr.split('.'); - x1 = x[0]; - x2 = x.length > 1 ? '.' + x[1] : ''; - var rgx = /(\d+)(\d{3})/; - var z = 0; - var len = String(x1).length; - var num = parseInt((len / 2) - 1); - - while (rgx.test(x1)) { - if (z > 0) { - x1 = x1.replace(rgx, '$1' + ',' + '$2'); - } else { - x1 = x1.replace(rgx, '$1' + ',' + '$2'); - rgx = /(\d+)(\d{2})/; - } - z++; - num--; - if (num == 0) { - break; + function inr_currency(nStr) { + nStr += ''; + x = nStr.split('.'); + x1 = x[0]; + x2 = x.length > 1 ? '.' + x[1] : ''; + var rgx = /(\d+)(\d{3})/; + var z = 0; + var len = String(x1).length; + var num = parseInt((len / 2) - 1); + + while (rgx.test(x1)) { + if (z > 0) { + x1 = x1.replace(rgx, '$1' + ',' + '$2'); + } else { + x1 = x1.replace(rgx, '$1' + ',' + '$2'); + rgx = /(\d+)(\d{2})/; + } + z++; + num--; + if (num == 0) { + break; + } } + return x1 + x2; } - return x1 + x2; -} // GLOBAL CURRENCY FORMAT -function format_currency(n) { - return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"); -} - -function dollartToInr() { - var posturl = 'http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json'; - var earningsdollar, finalearnings, convertrate; - - var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange; - - // Use jQuery.ajax to get the latest exchange rates, with JSONP: + function format_currency(n) { + return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"); + } - $.getJSON(conversionurl, function(data) { - convertrate = data.rates[currency]; /* * 0.975 Midmarket rate*/ - - console.log(data); + function dollartToInr() { + var posturl = 'http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json'; + var earningsdollar, finalearnings, convertrate; - $.getJSON(posturl, function(data) { - earningsdollar = data.account.available_earnings; /*- 3 payoneer commision $3*/ - finalearnings = earningsdollar * convertrate; + var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange; - if (currency == 'INR') { - currency_sign = '₹'; - } else if (currency == 'EUR') { - currency_sign = '€'; - } else if (currency == 'GBP') { - currency_sign = '£'; - } else { - currency_sign = currency; - } + // Use jQuery.ajax to get the latest exchange rates, with JSONP: + if(cache_currency_rate == 'false') { + $.getJSON(conversionurl, function (data) { + convertrate = data.rates[currency]; + /* * 0.975 Midmarket rate*/ - if (currency == 'INR') { - $('.header-logo-account__balance').text(currency_sign + ' ' + inr_currency(finalearnings.toFixed(2))).parent().attr('title', 'Actual Earnings: $' + earningsdollar); - } else { - $('.header-logo-account__balance').text(currency_sign + ' ' + format_currency(finalearnings)).parent().attr('title', 'Actual Earnings: $' + earningsdollar); - } + //console.log(data); - }); - }); -} + $.getJSON(posturl, function (data) { + earningsdollar = data.account.available_earnings; + /*- 3 payoneer commision $3*/ + finalearnings = earningsdollar * convertrate; -function convertPrice(unconverted_price, handleData) { - var conversion_rate, converted_price; + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; + } else { + currency_sign = currency; + } - if($.type(unconverted_price) === 'string') { - unconverted_price = unconverted_price.replace(/[^0-9\.]/g, ''); - } + if (currency == 'INR') { + $('.header-logo-account__balance').text(currency_sign + ' ' + inr_currency(finalearnings.toFixed(2))).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } else { + $('.header-logo-account__balance').text(currency_sign + ' ' + format_currency(finalearnings)).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } - $.ajax({ - url: 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange, - success:function(data){ + }); + }); + } else { + var exchange_rate = currency_rate.split('||') || 1; + + var last_cached_time = exchange_rate[0]; + var last_exchange_rate = exchange_rate[1]; + var current_timestamp = Math.floor(Date.now() / 1000); + var day_ago_time = current_timestamp - 86400; + + if(exchange_rate[2] != currency || last_cached_time < day_ago_time) { + // Fetch latest currency rate + var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange; + $.getJSON(conversionurl, function(data) { + save_option('currency_rate', current_timestamp+'||'+data.rates[currency]+'||'+currency); + + $.getJSON(posturl, function (data) { + earningsdollar = data.account.available_earnings; + /*- 3 payoneer commision $3*/ + finalearnings = earningsdollar * data.rates[currency]; + + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; + } else { + currency_sign = currency; + } - conversion_rate = data.rates[currency]; + if (currency == 'INR') { + $('.header-logo-account__balance').text(currency_sign + ' ' + inr_currency(finalearnings.toFixed(2))).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } else { + $('.header-logo-account__balance').text(currency_sign + ' ' + format_currency(finalearnings)).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } - if (currency == 'INR') { - currency_sign = '₹'; - } else if (currency == 'EUR') { - currency_sign = '€'; - } else if (currency == 'GBP') { - currency_sign = '£'; + }); + }); } else { - currency_sign = currency; - } + $.getJSON(posturl, function (data) { + earningsdollar = data.account.available_earnings; + /*- 3 payoneer commision $3*/ + finalearnings = earningsdollar * currency_rate.split('||')[1]; + + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; + } else { + currency_sign = currency; + } - converted_price = unconverted_price * conversion_rate; + if (currency == 'INR') { + $('.header-logo-account__balance').text(currency_sign + ' ' + inr_currency(finalearnings.toFixed(2))).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } else { + $('.header-logo-account__balance').text(currency_sign + ' ' + format_currency(finalearnings)).parent().attr('title', 'Actual Earnings: $' + earningsdollar); + } - if (currency == 'INR') { - converted_price = currency_sign + ' ' + inr_currency(converted_price.toFixed(2)); - } else { - converted_price = currency_sign + ' ' + format_currency(converted_price); + }); } - - handleData(converted_price); } - }); -} - -$(document).ready(function() { + } - // REMOVE STATEMENTS - AUTHOR FEE - // CONVERT CURRENCIES + function convertPrice(unconverted_price, handleData) { + var conversion_rate, converted_price; - if (hide_statement != 'false') { - var pathname = window.location.pathname; - var amount = 0; - var amount_string = ''; - var order_id = 0; - var next_amount = ''; + if($.type(unconverted_price) === 'string') { + unconverted_price = unconverted_price.replace(/[^0-9\.]/g, ''); + } - var unconverted, converted, current_object = [], conversion_rate; + if(cache_currency_rate == 'false') { + $.ajax({ + url: 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange, + success: function (data) { - if (pathname.indexOf('statement') > -1) { - $("#stored_statement").find("tr").each(function(i) { + conversion_rate = data.rates[currency]; - if ($(this).find("td").eq(3).find("span").text() == "Author Fee") { + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; + } else { + currency_sign = currency; + } - order_id = $(this).find('.statement__order_id').text(); - amount_string = $(this).find('.statement__amount').text(); - amount = parseFloat(amount_string.substring(1, amount_string.length)); - next_amount = $(this).next().find('.statement__amount').text(); - next_amount = parseFloat(next_amount.substring(1, next_amount.length)); + converted_price = unconverted_price * conversion_rate; - amount = amount + next_amount; + if (currency == 'INR') { + converted_price = currency_sign + ' ' + inr_currency(converted_price.toFixed(2)); + } else { + converted_price = currency_sign + ' ' + format_currency(converted_price); + } - $(this).next().find('.statement__amount').text('$' + amount.toFixed(2)); - $(this).hide(); + handleData(converted_price); } - - if(localise_earnings_table != 'false') { - if (openexchange == 'undefined') { - return false; + }); + } else { + var exchange_rate = currency_rate.split('||') || 1; + + var last_cached_time = exchange_rate[0]; + var last_exchange_rate = exchange_rate[1]; + var current_timestamp = Math.floor(Date.now() / 1000); + var day_ago_time = current_timestamp - 86400; + + if(exchange_rate[2] != currency || last_cached_time < day_ago_time) { + // Fetch latest currency rate + var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + openexchange; + $.getJSON(conversionurl, function(data) { + save_option('currency_rate', current_timestamp+'||'+data.rates[currency]+'||'+currency); + + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; } else { - if ($(this).is(':visible')) { - current_object[i] = $(this); + currency_sign = currency; + } - if (current_object[i].find('td').eq(7).text() != '') { - unconverted = current_object[i].find('td').eq(7).text(); - unconverted = parseFloat(unconverted.substring(1, unconverted.length)); + converted_price = unconverted_price * currency_rate.split('||')[1]; - converted = convertPrice(unconverted, function(data){ - current_object[i].find('td').eq(7).text(data).attr('title', 'Actual Earnings: $' + unconverted); - }); - } - if (current_object[i].find('td').eq(8).text() != '') { - unconverted = current_object[i].find('td').eq(8).text(); - unconverted = unconverted.substring(1, unconverted.length); - converted = convertPrice(unconverted, function(data){ - current_object[i].find('td').eq(8).text(data).attr('title', 'Actual Earnings: $' + unconverted); - }); - } - } + if (currency == 'INR') { + converted_price = currency_sign + ' ' + inr_currency(converted_price.toFixed(2)); + } else { + converted_price = currency_sign + ' ' + format_currency(converted_price); } + + handleData(converted_price); + }); + } else { + if (currency == 'INR') { + currency_sign = '₹'; + } else if (currency == 'EUR') { + currency_sign = '€'; + } else if (currency == 'GBP') { + currency_sign = '£'; + } else { + currency_sign = currency; } - }); - } - } - // CONVERT CURRENCIES IN EARNINGS TAB - if(localise_earnings_page != 'false') { - var pathname = window.location.pathname; - if(pathname.indexOf('/earnings/') > -1) { - - // Generate new graph - var graph_data = $.parseJSON($('body script[id="graphdata"]').text()); - var current_object, unconverted, converted, data_indexes, counter; - counter = 0; - data_indexes = graph_data.datasets[0]['data'].length; - - $.each(graph_data.datasets[0]['data'], function(i, val){ - if(val > 0) { - // Localize - if (openexchange == 'undefined') { - return false; - } else { - current_object = $(this); - unconverted = parseFloat(val); + converted_price = unconverted_price * currency_rate.split('||')[1]; - converted = convertPrice(unconverted, function (data) { - if (parseFloat(data.replace(/[^0-9\.]/g, '')) > 0) { - graph_data.datasets[0]['data'][i] = parseFloat(data.replace(/[^0-9\.]/g, '')); - counter++; - } - }); - } + if (currency == 'INR') { + converted_price = currency_sign + ' ' + inr_currency(converted_price.toFixed(2)); } else { - counter++; + converted_price = currency_sign + ' ' + format_currency(converted_price); } - }); - var renderGraph = setInterval(function(){ - if(data_indexes == counter) { - $('.-sales').text('Sales Earnings ('+currency+')'); + handleData(converted_price); + } + } + } - var chart_data = { - animationEasing: "easeInOutCirc", - animationSteps: 60, - scaleFontSize: 12, - scaleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", - scaleLabel: "<%=parseFloat(value).toLocaleString('en-EN', {style: 'currency', currency: '"+currency+"', minimumFractionDigits: 2})%>", - scaleStartValue: 0, - showTooltips: !1, - bezierCurve: !1 - }; + $(document).ready(function() { - $(".js-graph__canvas").remove(); - $('.graph__container').append(''); + // REMOVE STATEMENTS - AUTHOR FEE + // CONVERT CURRENCIES - var canvas = $(".graph__canvas").get(0).getContext('2d'); - new Chart(canvas).Line(graph_data, chart_data); + if (hide_statement != 'false') { + var pathname = window.location.pathname; + var amount = 0; + var amount_string = ''; + var order_id = 0; + var next_amount = ''; - clearInterval(renderGraph); - } - }, 100); + var unconverted, converted, current_object = [], conversion_rate; - // Convert headers (This month, balance, total value) - convertPrice($('.earnings-widget__amount:eq(0)').text().substr(1), function(data){ - $('.earnings-widget__amount:eq(0)').text(data); - }); - convertPrice($('.earnings-widget__amount:eq(1)').text().substr(1), function(data){ - $('.earnings-widget__amount:eq(1)').text(data); - }); - convertPrice($('.earnings-widget__amount:eq(2)').text().substr(1), function(data){ - $('.earnings-widget__amount:eq(2)').text(data); - }); - // Reduce font size to avoid design breakage in local currency - $('.earnings-widget__amount').css('font-size','30px'); - - // Convert prices in table - if(pathname.indexOf('/earnings/sales') > -1) { - $('.table-general tbody, tfoot').find('tr').each(function(index){ - current_object[index] = $(this); - convertPrice($(this).find('td').eq(2).text().substr(1), function(data){ - current_object[index].find('td').eq(2).text(data); - }); - }); - } else if(pathname.indexOf('/earnings/referrals') > -1) { - $('.table-general tbody, tfoot').find('tr').each(function(index){ - current_object[index] = $(this); - convertPrice($(this).find('td').eq(4).text().substr(1), function(data){ - current_object[index].find('td').eq(4).text(data); - }); - convertPrice($(this).find('td').eq(5).text().substr(1), function(data){ - current_object[index].find('td').eq(5).text(data); - }); - }); - } + if (pathname.indexOf('statement') > -1) { + $("#stored_statement").find("tr").each(function(i) { + + if ($(this).find("td").eq(3).find("span").text() == "Author Fee") { + + order_id = $(this).find('.statement__order_id').text(); + amount_string = $(this).find('.statement__amount').text(); + amount = parseFloat(amount_string.substring(1, amount_string.length)); + next_amount = $(this).next().find('.statement__amount').text(); + next_amount = parseFloat(next_amount.substring(1, next_amount.length)); + + amount = amount + next_amount; + + $(this).next().find('.statement__amount').text('$' + amount.toFixed(2)); + $(this).hide(); + } + + if(localise_earnings_table != 'false') { + if (openexchange == 'undefined') { + return false; + } else { + if ($(this).is(':visible')) { + current_object[i] = $(this); + + if (current_object[i].find('td').eq(7).text() != '') { + unconverted = current_object[i].find('td').eq(7).text(); + unconverted = parseFloat(unconverted.substring(1, unconverted.length)); + + converted = convertPrice(unconverted, function(data){ + current_object[i].find('td').eq(7).text(data).attr('title', 'Actual Earnings: $' + unconverted); + }); + } + if (current_object[i].find('td').eq(8).text() != '') { + unconverted = current_object[i].find('td').eq(8).text(); + unconverted = unconverted.substring(1, unconverted.length); + converted = convertPrice(unconverted, function(data){ + current_object[i].find('td').eq(8).text(data).attr('title', 'Actual Earnings: $' + unconverted); + }); + } + } + } + } + }); + } } - } - // SHOW LINKS IN REFERRALS PAGE - if(create_hrefs != 'false') { - var pathname = window.location.pathname; - if (pathname.indexOf('/referrals') > -1) { - var source = ''; - var path = ''; - var url = ''; - - var ifTableExists = setInterval(function () { - if ($('#results').length) { - clearInterval(ifTableExists); - $('#results').find('tr').each(function () { - if ($(this).find('td').eq(1).text() != '(not set)') { - source = $(this).find('td').eq(0).text(); - path = $(this).find('td').eq(1).text(); - - url = '' + path + ''; - - $(this).find('td').eq(1).html(url); + // CONVERT CURRENCIES IN EARNINGS TAB + if(localise_earnings_page != 'false') { + var pathname = window.location.pathname; + if(pathname.indexOf('/earnings/') > -1) { + + // Generate new graph + var graph_data = $.parseJSON($('body script[id="graphdata"]').text()); + var current_object, unconverted, converted, data_indexes, counter; + counter = 0; + data_indexes = graph_data.datasets[0]['data'].length; + + $.each(graph_data.datasets[0]['data'], function(i, val){ + if(val > 0) { + // Localize + if (openexchange == 'undefined') { + return false; + } else { + current_object = $(this); + unconverted = parseFloat(val); + + converted = convertPrice(unconverted, function (data) { + if (parseFloat(data.replace(/[^0-9\.]/g, '')) > 0) { + graph_data.datasets[0]['data'][i] = parseFloat(data.replace(/[^0-9\.]/g, '')); + counter++; + } + }); } + } else { + counter++; + } + }); + + var renderGraph = setInterval(function(){ + if(data_indexes == counter) { + $('.-sales').text('Sales Earnings ('+currency+')'); + + var chart_data = { + animationEasing: "easeInOutCirc", + animationSteps: 60, + scaleFontSize: 12, + scaleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", + scaleLabel: "<%=parseFloat(value).toLocaleString('en-EN', {style: 'currency', currency: '"+currency+"', minimumFractionDigits: 2})%>", + scaleStartValue: 0, + showTooltips: !1, + bezierCurve: !1 + }; + + $(".js-graph__canvas").remove(); + $('.graph__container').append(''); + + var canvas = $(".graph__canvas").get(0).getContext('2d'); + new Chart(canvas).Line(graph_data, chart_data); + + clearInterval(renderGraph); + } + }, 100); + + // Convert headers (This month, balance, total value) + convertPrice($('.earnings-widget__amount:eq(0)').text().substr(1), function(data){ + $('.earnings-widget__amount:eq(0)').text(data); + }); + convertPrice($('.earnings-widget__amount:eq(1)').text().substr(1), function(data){ + $('.earnings-widget__amount:eq(1)').text(data); + }); + convertPrice($('.earnings-widget__amount:eq(2)').text().substr(1), function(data){ + $('.earnings-widget__amount:eq(2)').text(data); + }); + // Reduce font size to avoid design breakage in local currency + $('.earnings-widget__amount').css('font-size','30px'); + + // Convert prices in table + if(pathname.indexOf('/earnings/sales') > -1) { + $('.table-general tbody, tfoot').find('tr').each(function(index){ + current_object[index] = $(this); + convertPrice($(this).find('td').eq(2).text().substr(1), function(data){ + current_object[index].find('td').eq(2).text(data); + }); + }); + } else if(pathname.indexOf('/earnings/referrals') > -1) { + $('.table-general tbody, tfoot').find('tr').each(function(index){ + current_object[index] = $(this); + convertPrice($(this).find('td').eq(4).text().substr(1), function(data){ + current_object[index].find('td').eq(4).text(data); + }); + convertPrice($(this).find('td').eq(5).text().substr(1), function(data){ + current_object[index].find('td').eq(5).text(data); + }); }); } - }, 100); + } + } + + // SHOW LINKS IN REFERRALS PAGE + if(create_hrefs != 'false') { + var pathname = window.location.pathname; + if (pathname.indexOf('/referrals') > -1) { + var source = ''; + var path = ''; + var url = ''; + + var ifTableExists = setInterval(function () { + if ($('#results').length) { + clearInterval(ifTableExists); + $('#results').find('tr').each(function () { + if ($(this).find('td').eq(1).text() != '(not set)') { + source = $(this).find('td').eq(0).text(); + path = $(this).find('td').eq(1).text(); + + url = '' + path + ''; + + $(this).find('td').eq(1).html(url); + } + }); + } + }, 100); + } } - } - // VERIFY PURCHASE + // VERIFY PURCHASE - if (verify_purchase != 'false') { - var pathname = window.location.pathname; - if (pathname.indexOf('author_dashboard') > -1) { - var verify_html_block = '

Verify Purchase Code

'; + if (verify_purchase != 'false') { + var pathname = window.location.pathname; + if (pathname.indexOf('author_dashboard') > -1) { + var verify_html_block = '

Verify Purchase Code

'; - //$("#content .content-s").append(verify_html_block); - $(verify_html_block).insertBefore("#content .content-s .content-s"); - //alert('done'); + //$("#content .content-s").append(verify_html_block); + $(verify_html_block).insertBefore("#content .content-s .content-s"); + //alert('done'); + } } - } - $("#verifypurchase").submit(function(e) { - e.preventDefault(); - var purchase_code = $("#purchase_code"); - var flag = false; - if (purchase_code.val() == "") { - purchase_code.focus(); - flag = false; - return false; - } else { - flag = true; - } - var item_purchase_code = purchase_code.val(); - $(".loading").fadeIn("slow").html("

Please wait...

"); + $("#verifypurchase").submit(function(e) { + e.preventDefault(); + var purchase_code = $("#purchase_code"); + var flag = false; + if (purchase_code.val() == "") { + purchase_code.focus(); + flag = false; + return false; + } else { + flag = true; + } + var item_purchase_code = purchase_code.val(); + $(".loading").fadeIn("slow").html("

Please wait...

"); - var posturl = 'http://marketplace.envato.com/api/v3/' + username + '/' + apikey + '/verify-purchase:' + item_purchase_code + '.json'; + var posturl = 'http://marketplace.envato.com/api/v3/' + username + '/' + apikey + '/verify-purchase:' + item_purchase_code + '.json'; - $.ajax({ - type: 'GET', - url: posturl, - data: { - get_param: 'value' - }, - dataType: 'json', - success: function(data) { + $.ajax({ + type: 'GET', + url: posturl, + data: { + get_param: 'value' + }, + dataType: 'json', + success: function(data) { - if (data['verify-purchase'].buyer == '' || data['verify-purchase'].buyer == null) { + if (data['verify-purchase'].buyer == '' || data['verify-purchase'].buyer == null) { - $('.loading').fadeIn('slow').html('

Sorry. That was a wrong verification code!

'); + $('.loading').fadeIn('slow').html('

Sorry. That was a wrong verification code!

'); - } else if (data.code == 'not_authenticated') { - $('.loading').fadeIn('slow').html('

Sorry. Username and/or API Key is invalid.

'); - } else { + } else if (data.code == 'not_authenticated') { + $('.loading').fadeIn('slow').html('

Sorry. Username and/or API Key is invalid.

'); + } else { - var buyer = data['verify-purchase'].buyer; - var item_name = data['verify-purchase'].item_name; - var licence = data['verify-purchase'].licence; - var timestamp = data['verify-purchase'].created_at; + var buyer = data['verify-purchase'].buyer; + var item_name = data['verify-purchase'].item_name; + var licence = data['verify-purchase'].licence; + var timestamp = data['verify-purchase'].created_at; - $('.loading').fadeIn('slow').html('

' + buyer + ' purchased a ' + licence + ' of ' + item_name + ' ' + $.timeago(timestamp) + '

'); + $('.loading').fadeIn('slow').html('

' + buyer + ' purchased a ' + licence + ' of ' + item_name + ' ' + $.timeago(timestamp) + '

'); - } + } - }, + }, - error: function(data) { - $('.loading').fadeIn('slow').html('

Sorry. Something went wrong!

'); - } + error: function(data) { + $('.loading').fadeIn('slow').html('

Sorry. Something went wrong!

'); + } - }); + }); - }); + }); // Hide Author Earnings // Author is browsing in public and do not want to reveal his balance -if (hide_earnings == 'true') { - $('.header-logo-account__balance').hide(); -} + if (hide_earnings == 'true') { + $('.header-logo-account__balance').hide(); + } -}); /*End Document Ready*/ + }); /*End Document Ready*/ -var current_url = window.location.pathname; + var current_url = window.location.pathname; -$(document).click(function() { - if(window.location.pathname.indexOf('/earnings/') > -1) { - if (current_url != window.location.pathname) { - location.reload(); + $(document).click(function() { + if(window.location.pathname.indexOf('/earnings/') > -1) { + if (current_url != window.location.pathname) { + location.reload(); + } } - } + }); }); \ No newline at end of file diff --git a/css/options.css b/css/options.css index f4cae03..7b0f51b 100644 --- a/css/options.css +++ b/css/options.css @@ -1,124 +1,124 @@ /*Options Page*/ body { - background:#eee; - font-family:Arial, Helvetica, Sans-serif; - font-size:14px; - color:#3D3D3D; + background:#eee; + font-family:Arial, Helvetica, Sans-serif; + font-size:14px; + color:#3D3D3D; } .settings { - background:#FFF; - margin:50px auto; - padding:30px; - max-width:800px; + background:#FFF; + margin:50px auto; + padding:30px; + max-width:800px; } h1 { - text-align:center; - color:#313131; + text-align:center; + color:#313131; } p { - color:#707070; + color:#707070; } a { - color:#53A2EC; - text-decoration:none; + color:#53A2EC; + text-decoration:none; } a:hover { - color:#2473BD; + color:#2473BD; } hr { - -moz-box-sizing: content-box; - box-sizing: content-box; - height: 0; - margin-top: 20px; - margin-bottom: 20px; - border: 0; - border-top: 1px solid #eee; + -moz-box-sizing: content-box; + box-sizing: content-box; + height: 0; + margin-top: 20px; + margin-bottom: 20px; + border: 0; + border-top: 1px solid #eee; } #options-area { - margin-top:50px; + margin-top:50px; } .text-center { - text-align:center; -} + text-align:center; +} .control-group { - margin-bottom:20px; + margin-bottom:20px; } .control-label { - float:left; - margin:7px; + float:left; + margin:7px; } .help-block { - font-size:12px; + font-size:12px; } .controls { - margin-left:250px; + margin-left:250px; } .controls input[type=text], .controls select { - padding:5px 10px; - border:solid 1px #CCC; - width:300px; + padding:5px 10px; + border:solid 1px #CCC; + width:300px; } select[disabled], input[disabled] { -cursor: not-allowed; -background-color: #eee; -opacity: 1; + cursor: not-allowed; + background-color: #eee; + opacity: 1; } .btn { -display: inline-block; -padding: 6px 12px; -margin-bottom: 0; -font-size: 14px; -font-weight: 400; -line-height: 1.42857143; -text-align: center; -white-space: nowrap; -vertical-align: middle; -cursor: pointer; --webkit-user-select: none; --moz-user-select: none; --ms-user-select: none; -user-select: none; -background-image: none; -border: 1px solid transparent; -border-radius: 4px; -color: #939393; --webkit-transition:all .1s linear; -transition:all .1s linear; -outline:none; + display: inline-block; + padding: 6px 12px; + margin-bottom: 0; + font-size: 14px; + font-weight: 400; + line-height: 1.42857143; + text-align: center; + white-space: nowrap; + vertical-align: middle; + cursor: pointer; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + background-image: none; + border: 1px solid transparent; + border-radius: 4px; + color: #939393; + -webkit-transition:all .1s linear; + transition:all .1s linear; + outline:none; } .btn-success { -background-color: #2cc76a; -background-image: none; -border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); -color:#FFF; + background-color: #2cc76a; + background-image: none; + border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); + color:#FFF; } .highlight { -background: #f5e9a3; -padding: 1em 1.5em; --webkit-transition: background .1s linear; -transition: background .1s linear; + background: #f5e9a3; + padding: 1em 1.5em; + -webkit-transition: background .1s linear; + transition: background .1s linear; } .highlight.love { -background: #f5a3cc; + background: #f5a3cc; } diff --git a/manifest.json b/manifest.json index c078774..4d3c330 100644 --- a/manifest.json +++ b/manifest.json @@ -1,26 +1,50 @@ { - "content_scripts": [ { - "js": [ "js/jquery.min.js", "js/plugins.js", "js/Chart.min.js", "better-envato.js" ], - "css": ["css/envato-styles.css"], - "matches": [ "*://*.themeforest.net/*", "*://*.codecanyon.net/*", "*://*.videohive.net/*", "*://*.audiojungle.net/*", "*://*.graphicriver.net/*", "*://*.photodune.net/*", "*://*.3docean.net/*", "*://*.activeden.net/*", "*://*.envato.com/*" ] - } ], -"browser_action": { - "name": "Better Envato" - }, -"background": { - "page": "background.html" - }, - "description": "Make Envato better by adding some cool features", - "icons": { - "128": "img/128.png", - "16": "img/16.png", - "48": "img/48.png" - }, - "manifest_version": 2, - "name": "Better Envato", - "short_name": "Better Envato", - "options_page": "options.html", - "permissions": [ "webRequest", "notifications", "background" ], - "update_url": "http://clients2.google.com/service/update2/crx", - "version": "1.2.5" -} + "content_scripts":[ + { + "js":[ + "js/jquery.min.js", + "js/plugins.js", + "js/Chart.min.js", + "better-envato.js" + ], + "css":[ + "css/envato-styles.css" + ], + "matches":[ + "*://*.themeforest.net/*", + "*://*.codecanyon.net/*", + "*://*.videohive.net/*", + "*://*.audiojungle.net/*", + "*://*.graphicriver.net/*", + "*://*.photodune.net/*", + "*://*.3docean.net/*", + "*://*.activeden.net/*", + "*://*.envato.com/*" + ] + } + ], + "browser_action":{ + "name":"Better Envato" + }, + "background":{ + "page":"background.html" + }, + "description":"Make Envato better by adding some cool features", + "icons":{ + "128":"img/128.png", + "16":"img/16.png", + "48":"img/48.png" + }, + "manifest_version":2, + "name":"Better Envato", + "short_name":"Better Envato", + "options_page":"options.html", + "permissions":[ + "storage", + "webRequest", + "notifications", + "background" + ], + "update_url":"http://clients2.google.com/service/update2/crx", + "version":"1.2.5" +} \ No newline at end of file diff --git a/options.html b/options.html index 0743d83..8eaa5b5 100644 --- a/options.html +++ b/options.html @@ -1,340 +1,346 @@ -Better Envato Options - + Better Envato Options +
-

Better Envato Options

- - -
- -
-
-
-
- -
- -
-
-
- -
- -
-
-
-
- -
-
- -
-
- -
-
- -
-
-
-
-
- -
-
- -
-
- -
-
- -
-
-
-
-
- -
-
- -
-
- -
-
- -
-

Please Fill below details to localize earnings

-
-
-
- -
- -

Signup free to get API Key from Open Exchange rate

-
-
-
- -
- -
-
-
-
- -
-
- -
-
-
-
-
- -
-
- -
-
-
-
-
- -
-
- +

Better Envato Options

+ + +
+ +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+

Please Fill below details to localize earnings

+
+
+
+ +
+ +

Signup free to get API Key from Open Exchange rate

+
+
+
+ +
+ +
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
-
-
-
- -
-
- -
-
+
+
-
-
-
-
-
-

Made with ♥ by Surjith S M (Envato Portfolio).
- Fork on Github | Found an issue? | Credits : Envato API | Contributors: Zan Gerden

+ Fork on Github | Found an issue? | Credits : Envato API | Contributors: Žan Gerden

- - + + \ No newline at end of file diff --git a/options.js b/options.js index 824ffd4..6bb2f06 100644 --- a/options.js +++ b/options.js @@ -5,109 +5,167 @@ function init_options() { // console.log("function: init_options"); + var current_object = []; + //load currently stored options configuration var $inputs = $('#options-area :input'); $inputs.each(function() { - if (typeof localStorage[this.name] != 'undefined') { - $(this).val(localStorage[this.name]); - } + current_object[$(this).attr('name')] = $(this); + + get_option($(this).attr('name'), function(value, name) { + current_object[name].val(value); + }); }); var $checkboxes = $('#options-area :input[type=checkbox]'); $checkboxes.each(function() { - if (localStorage[this.name] == 'false') { - //alert(localStorage[this.name]) - $(this).attr('checked', false); - } else if (localStorage[this.name] == 'true') { - $(this).attr('checked', true); - } + current_object[$(this).attr('name')] = $(this); + + get_option($(this).attr('name'), function(value, name){ + if(value == 'false') { + current_object[name].attr('checked', false); + } else if (value == 'true') { + current_object[name].attr('checked', true); + } + }); }); + get_option('currency', function(value, name){ + $('select[name='+name+']').select(value); + }); } function save_options() { // console.log("function: save_options"); + chrome.storage.sync.clear(); $("input[type=text],select,textarea").each(function() { - localStorage[$(this).attr("name")] = $(this).val(); + var name = $(this).attr('name'); + var value = $(this).val(); + + save_option(name, value); }); $("input[type=checkbox]").each(function() { - localStorage[$(this).attr("name")] = $(this).prop("checked"); + var name = $(this).attr('name'); + var value = $(this).prop('checked').toString(); + + save_option(name, value); + + if(name == 'cache_currency_rate') { + // Fetch latest currency rate + get_option('openexchange', function(apikey){ + var conversionurl = 'http://openexchangerates.org/api/latest.json?app_id=' + apikey; + var current_timestamp = Math.floor(Date.now() / 1000); + $.getJSON(conversionurl, function(data) { + get_option('currency', function(currency){ + save_option('currency_rate', current_timestamp+'||'+data.rates[currency]); + }); + }); + }); + } }); - $(this).text('Saving...').removeClass("btn-success"); - - setTimeout(function() { - $('#save-options-button').text('Options Saved'); + $(this).text('Saving...').removeClass("btn-success"); + + setTimeout(function() { + $('#save-options-button').text('Options Saved'); }, 700); - - - // Ask for a good review - if( ! localStorage.reviewed ) { - $('#rate-it').html('
Enjoying Better Envato? Head over to the Chrome Web Store and give it 5 stars. We will love you forever.
'); - } + get_option('reviewed', function(value){ + if(value != 'true') { + $('#rate-it').html('
Enjoying Better Envato? Head over to the Chrome Web Store and give it 5 stars. We will love you forever.
'); + } + }); get_sales_data(); get_comment_data(); chrome.extension.getBackgroundPage().updatedSettings(); +} +/** + * Saves option to Chrome.storage + */ +function save_option(name, value){ + var object = {}; + object[name] = value; + + chrome.storage.sync.set(object, function() { + if(chrome.extension.lastError) { + console.log('An error occured: ' + chrome.extension.lastError.message); + return false; + } else { + return true; + } + }); } +/** + * Gets option from Chrome.storage + */ +function get_option(name, callback) { + chrome.storage.sync.get(name, function(response) { + callback(response[name], name); + }) +} /** * Reset the save button */ $('input, select').on('keyup click', function () { - $('#save-options-button').text('Save Options').addClass("btn-success"); + $('#save-options-button').text('Save Options').addClass("btn-success"); }); /** * Store that the user has already been to the Web Store (:. we <3 them) */ - + $('body').on('click', '.fivestars', function () { - localStorage.reviewed = true; - $('#rate-it').html('
You\'re awesome ♥'); + save_option('reviewed', 'true'); + $('#rate-it').html('
You\'re awesome ♥'); }); - - - // Sales Notifications function get_sales_data() { - var username = localStorage.username; - var apikey = localStorage.apikey; - var earnings = localStorage.earnings; - - if (typeof username == 'undefined' || typeof apikey == 'undefined') { - return false; - } - - // Use Envato API to check sales - $.get('http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json', function(data) { - - //get current sales - var new_earnings = data.account.available_earnings; - localStorage.earnings = new_earnings; - + get_option('username', function(value) { + if(typeof value == 'undefined') { + return false; + } + var username = value; + get_option('apikey', function(value) { + if(typeof value == 'undefined') { + return false; + } + var apikey = value; + get_option('earnings', function(value) { + var earnings = value; + + // Use Envato API to check sales + $.get('http://marketplace.envato.com/api/edge/' + username + '/' + apikey + '/account.json', function(data) { + + //get current sales + var new_earnings = data.account.available_earnings; + save_option('earnings', new_earnings); + + }); + }); + }); }); - } - function get_comment_data() { - var username = localStorage.username; + get_option('username', function(value) { + var username = value; - $.get('http://themeforest.net/feeds/user_item_comments/' + username + '.atom', function(data) { - var comment_feed = $.xml2json(data); - //console.log(comment_feed); - var comment_id = comment_feed.entry[0].id; - localStorage.new_comment_id = comment_id; + $.get('http://themeforest.net/feeds/user_item_comments/' + username + '.atom', function(data) { + var comment_feed = $.xml2json(data); + //console.log(comment_feed); + var comment_id = comment_feed.entry[0].id; + save_option('new_comment_id', comment_id); + }); }); }