-
Notifications
You must be signed in to change notification settings - Fork 1
/
extern_rtc.js
145 lines (130 loc) · 4.33 KB
/
extern_rtc.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
// functionality of the plugin
var ExternFunction = (function() {
var liTitle = chrome.i18n.getMessage("liTitle"),
liText = chrome.i18n.getMessage("liText"),
titleText = chrome.i18n.getMessage("extName"),
retweetText = chrome.i18n.getMessage("retweetText"),
//rtc li element
rtcLiElement,
//to save the new tweet original title
previusTitle = '',
tweetButton,
tweetDialog,
tweetDialogTitle,
tweetDialogContent,
elementToObserv,
allElementLoaded = false,
closeModalNewTweetElement,
filterPromoted = false,
//action when rtc li element is clicked
clickRtc = function (e) {
//avoid to execute "a" default action
e.preventDefault();
var target = e.target,
//tweet element
parent = $(target).closest('.content'),
//tweet text
tweetText = parent.find('.js-tweet-text').text(),
//twitter user
twitterUser = retweetText + parent.find('span.username').find('b').text()+ ' ';
//click to open new tweet modal
tweetButton.click();
//save previus title
previusTitle = tweetDialogTitle.text();
//change modal title,add class to title elemento to find it later and remove orginal title class and center new title
tweetDialogTitle.text(titleText)
.addClass('rtcTitle')
.removeClass('modal-title')
.css('text-align','center');
chrome.storage.local.get('auto_message_active', function(data) {
if (data.auto_message_active) {
chrome.storage.local.get('auto_text', function(data) {
var autoMessage = (data.auto_text)?data.auto_text:'';
//add retweet text
tweetDialogContent.find('div').text(autoMessage+twitterUser+tweetText);
});
}else{
//add retweet text
tweetDialogContent.find('div').text(twitterUser+tweetText);
}
});
},
//function to set original title, class and empty content
clickNewTweet = function (e) {
if(previusTitle !== '') {
//remove center title, add original title class, remove temporal "rtc" title class and set original title
$(tweetDialogTitle).css('text-align','')
.addClass('modal-title')
.removeClass('rtcTitle')
.text(previusTitle);
//remove retweet text
tweetDialogContent.find('div').empty().html('<br/>');
previusTitle = '';
tweetDialogContent.focus();
}
},
//function to add li rtc elements and its actions
addOptionsAndClickEvents = function (elements) {
var actionsFooter = elements.find('.js-actions:not(.rtc)');
if(actionsFooter.length !== 0) {
//create rt+c element
if(!rtcLiElement) {
rtcLiElement = actionsFooter.find('.ProfileTweet-action--reply').eq(0).clone()
.addClass('js-rtc')
rtcLiElement.find('.Icon--reply').addClass('username').text(liText);
rtcLiElement.find('.js-tooltip').attr('title', liTitle);
}
//add rt+c element
actionsFooter.addClass('rtc').prepend(rtcLiElement);
}
if(!allElementLoaded) {
//add click Rt+C
elementToObserv.on('click', '.js-rtc', clickRtc);
}
},
filterPromoted = function (tweets) {
chrome.storage.local.get('filter_promoted', function(data) {
var promoted;
if(data.filter_promoted) {
promoted = tweets.find('.js-action-profile-promoted').closest('.js-stream-item');
promoted.remove();
promoted = tweets.find('.js-promoted-badge').closest('.js-stream-item');;
promoted.remove();
}
});
},
//load shared elements
loadElements = function () {
tweetButton = $('#global-new-tweet-button');
tweetDialog = $('#global-tweet-dialog');
tweetDialogTitle = tweetDialog.find('.modal-title');
tweetDialogContent = $('#tweet-box-global');
elementToObserv = $('#page-outer');
closeModalNewTweetElement = $('.modal-close.js-close');
};
//public methods
return {
initPlugin : function () {
var parent, tweets;
//avoid profile page
if($('.profile.active').length === 0) {
loadElements();
//add listener to add new tweet elements
insertionQ('li.js-stream-item').summary(function(tweets) {
var jqtweets = $(tweets);
addOptionsAndClickEvents(jqtweets);
filterPromoted(jqtweets);
});
parent = $('.content-main');
addOptionsAndClickEvents(parent);
tweets = parent.find('.js-stream-item');
filterPromoted(tweets);
allElementLoaded = true;
//add listener to new tweet close button
closeModalNewTweetElement.on('click', clickNewTweet);
}
}
}
})();
//execute when load
ExternFunction.initPlugin();