-
Notifications
You must be signed in to change notification settings - Fork 32
/
vote_weight_slider_busy.js
194 lines (180 loc) · 8.71 KB
/
vote_weight_slider_busy.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var token_vote_weight_slider_busy = null;
var aut = null;
var rewardBalance = null;
var recentClaims = null;
var steemPrice = null;
var votePowerReserveRate = null;
var account = null;
var isFloatingFooterEnabled = null
var isPostPage = false;
var dollars = 0;
var retryCountVoteWeightSliderBusy = 0;
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.to == 'vote_weight_slider_busy') {
aut = request.data.user;
if (request.order === 'start' && token_vote_weight_slider_busy == null) {
token_vote_weight_slider_busy = request.token;
rewardBalance = request.data.rewardBalance;
recentClaims = request.data.recentClaims;
steemPrice = request.data.steemPrice;
votePowerReserveRate = request.data.votePowerReserveRate;
account = request.data.account;
isFloatingFooterEnabled = request.data.isPostFloatingBottomBarEnabled;
retryCountVoteWeightSliderBusy = 0;
isPostPage = false;
canstartVoteWeightSliderBusy();
} else if (request.order === "click" && token_vote_weight_slider_busy == request.token) {
retryCountVoteWeightSliderBusy = 0;
rewardBalance = request.data.rewardBalance;
recentClaims = request.data.recentClaims;
steemPrice = request.data.steemPrice;
votePowerReserveRate = request.data.votePowerReserveRate;
account = request.data.account;
isFloatingFooterEnabled = request.data.isPostFloatingBottomBarEnabled;
isPostPage = false;
canstartVoteWeightSliderBusy();
} else if (request.order === "notif" && token_vote_weight_slider_busy == request.token) {
retryCountVoteWeightSliderBusy = 0;
rewardBalance = request.data.rewardBalance;
recentClaims = request.data.recentClaims;
steemPrice = request.data.steemPrice;
votePowerReserveRate = request.data.votePowerReserveRate;
account = request.data.account;
}
}
});
function canstartVoteWeightSliderBusy() {
if (retryCountVoteWeightSliderBusy > 20) return;
if (regexPostBusy.test(window.location.href)) {
if (isFloatingFooterEnabled) {
if ($('.smi-post-floating-footer').length > 0 && $('.Buttons__link:has(.icon-praise_fill)').length > 0) {
isPostPage = true;
startVoteWeightSliderBusy();
} else
setTimeout(function() {
retryCountVoteWeightSliderBusy++;
canstartVoteWeightSliderBusy();
}, 1000);
} else {
if ($('.Buttons__link:has(.icon-praise_fill)').length > 0) {
startVoteWeightSliderBusy();
} else {
setTimeout(function() {
retryCountVoteWeightSliderBusy++;
canstartVoteWeightSliderBusy();
}, 1000);
}
}
} else if (regexFeedBusy.test(window.location.href) || regexBusy.test(window.location.href) || regexBlogBusy.test(window.location.href)) {
if ($('.Buttons__link:has(.icon-praise_fill)').length > 0) {
startVoteWeightSliderBusy();
} else {
setTimeout(function() {
retryCountVoteWeightSliderBusy++;
canstartVoteWeightSliderBusy();
}, 1000);
}
}
}
function startVoteWeightSliderBusy() {
$('.Buttons__link:has(.icon-praise_fill)').each(function() {
if (!$(this).hasClass('btnLikeUnlike')) {
if (!$(this).hasClass('active')) {
setupLikeButton($(this));
} else {
setupUnlikeButton($(this));
}
}
});
}
function setupLikeButton(btnBusy) {
if ($(btnBusy).parent().find('.btnLikeSP').length > 0)
$(btnBusy).parent().find('.btnLikeSP').show();
else {
var buttonLikeBusySP = $('<a role="presentation" class="Buttons__link btnLikeSP btnLikeUnlike"><i class="iconfont icon-praise_fill"></i></a>');
$(btnBusy).before(buttonLikeBusySP);
buttonLikeBusySP.unbind('click').click(function() {
createPopupVoteSlider($(this));
});
}
if (!$(btnBusy).hasClass('btnLikeUnlike'))
$(btnBusy).remove();
}
function setupUnlikeButton(btnBusy) {
if ($(btnBusy).parent().find('.btnUnlikeSP').length > 0)
$(btnBusy).parent().find('.btnUnlikeSP').show();
else {
var buttonUnlikeBusySP = $('<a role="presentation" class="active Buttons__link btnUnlikeSP btnLikeUnlike"><i class="iconfont icon-praise_fill"></i></a>');
$(btnBusy).before(buttonUnlikeBusySP);
buttonUnlikeBusySP.unbind('click').click(function() {
unlikePost($(this));
});
}
if (!$(btnBusy).hasClass('btnLikeUnlike'))
$(btnBusy).remove();
}
async function createPopupVoteSlider(element) {
var currentWeight = 100;
dollars = 0;
var popoverPosition = (isPostPage && isFloatingFooterEnabled ? 'top' : 'bottom');
$(element).attr('data-toggle', 'popover');
$(element).attr('data-content', '<div class="popup-slider-container"><div><span class="percentage-vote-slider-busy">100</span>% : <span class="value-vote-slider-busy">10</span>$</div><input id="vote-weight-slider-busy" data-slider-id="ex1Slider" type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100"/><button id="upvote-busy" class="btn upvote-slider-btn-busy">Upvote</button></div>');
$(element).attr('data-placement', popoverPosition);
$(element).attr('data-html', 'true');
$(element).attr('data-trigger', 'click');
$(element).popover('show');
dollars = await window.SteemPlus.Utils.getVotingDollarsPerAccount(100, account, rewardBalance, recentClaims, steemPrice, votePowerReserveRate, false);
$(element).parent().find('.value-vote-slider-busy').html(dollars.toFixed(3));
$(element).parent().find('#vote-weight-slider-busy').slider({
tooltip: 'hide'
});
$(element).parent().find('#vote-weight-slider-busy').on('change', async function(slideEvt) {
currentWeight = slideEvt.value.newValue;
dollars = await window.SteemPlus.Utils.getVotingDollarsPerAccount(currentWeight, account, rewardBalance, recentClaims, steemPrice, votePowerReserveRate, false);
$(element).parent().find('.percentage-vote-slider-busy').html(currentWeight);
$(element).parent().find('.value-vote-slider-busy').html(dollars.toFixed(3));
});
$(element).parent().find('.upvote-slider-btn-busy').on('click', function() {
if (isPostPage) {
var tmp = window.location.href.split('@')[1];
} else {
var tmp = $(element).parent().parent().parent().parent().parent().find('.Story__content__title').attr('href').split('@')[1];
}
var username = tmp.split('/')[0];
var permlink = tmp.split('/')[1];
sc2.vote(account.name, username, permlink, currentWeight * 100, function(err, res) {
if (err) console.log(err);
if (res) console.log(res);
if (res !== null) {
$(element).hide();
$('.popover').remove();
setupUnlikeButton($(element));
var oldCount = parseInt($(element).parent().find('.Buttons__reactions-count > span > span').html());
$(element).parent().find('.Buttons__reactions-count > span > span').eq(0).html(oldCount + 1);
var oldPayout = parseFloat($(element).parent().parent().find('.Payout > span > span > span').html());
$(element).parent().parent().find('.Payout > span > span > span').html((oldPayout + dollars).toFixed(2));
}
});
});
}
function unlikePost(btnBusy) {
if (isPostPage) {
var tmp = window.location.href.split('@')[1];
} else {
var tmp = $(btnBusy).parent().parent().parent().parent().parent().find('.Story__content__title').attr('href').split('@')[1];
}
var username = tmp.split('/')[0];
var permlink = tmp.split('/')[1];
sc2.vote(account.name, username, permlink, 0, function(err, res) {
if (err) console.log(err);
if (res) console.log(res);
if (res !== null) {
$(btnBusy).hide();
setupLikeButton($(btnBusy));
var oldCount = parseInt($(btnBusy).parent().find('.Buttons__reactions-count > span > span').html());
$(btnBusy).parent().find('.Buttons__reactions-count > span > span').eq(0).html(oldCount - 1);
var oldPayout = parseFloat($(btnBusy).parent().parent().find('.Payout > span > span > span').html());
$(btnBusy).parent().parent().find('.Payout > span > span > span').html((oldPayout - dollars).toFixed(2));
}
});
}