-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.js
120 lines (96 loc) · 3.61 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
** file: options.js
** description: javascript code for "options.html" page
*/
function init_options() {
// console.log("function: init_options");
//load currently stored options configuration
var $inputs = $('#options-area :input');
$inputs.each(function() {
if (typeof localStorage[this.name] != 'undefined') {
$(this).val(localStorage[this.name]);
}
});
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);
}
});
}
function save_options() {
// console.log("function: save_options");
$("input[type=text],select,textarea").each(function() {
localStorage[$(this).attr("name")] = $(this).val();
});
$("input[type=checkbox]").each(function() {
localStorage[$(this).attr("name")] = $(this).prop("checked");
});
$(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('<div class="highlight">Enjoying <strong>Better Envato</strong>? Head over to the <a class="fivestars" href="https://chrome.google.com/webstore/detail/better-envato/mlbkjbladkceacbifjpgimbkibhgbadf/reviews" target="_bank">Chrome Web Store</a> and give it 5 stars. We will love you <em>forever</em>.</div>');
}
get_sales_data();
get_comment_data();
chrome.extension.getBackgroundPage().updatedSettings();
}
/**
* Reset the save button
*/
$('input, select').on('keyup click', function() {
$('#save-options-button').text('Save Options').addClass("btn-success");
});
/**
* Store that the user has already been to the Web Store <3 )
*/
$('body').on('click', '.fivestars', function() {
localStorage.reviewed = true;
$('#rate-it').html('<div class="highlight love">You\'re <strong>awesome</strong> ♥');
});
// Sales Notifications
function get_sales_data() {
var username = localStorage.username;
var personal_token = localStorage.personal_token;
var earnings = localStorage.earnings;
if (typeof username == 'undefined' || typeof personal_token == 'undefined') {
return false;
}
// Use Envato API to check sales
var posturl = 'https://api.envato.com/v1/market/private/user/account.json';
jQuery.ajax({
url: posturl,
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + personal_token
},
success: function(data) {
//get current sales
var new_earnings = data.account.available_earnings;
localStorage.earnings = new_earnings;
localStorage.sales_stamp = '0';
},
error: function(data) {
response = JSON.parse(data.responseText);
console.log("Error: ", data);
}
});
}
function get_comment_data() {
var username = localStorage.username;
$.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;
});
}
//bind events to dom elements
document.addEventListener('DOMContentLoaded', init_options);
document.querySelector('#save-options-button').addEventListener('click', save_options);