-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
194 lines (175 loc) · 5.63 KB
/
background.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
chrome.runtime.setUninstallURL("https://1ce.org");
if (!localStorage.created) {
chrome.tabs.create({ url: "https://1ce.org" });
var manifest = chrome.runtime.getManifest();
localStorage.ver = manifest.version;
localStorage.created = 1;
}
var cssByElements = {
"like and share actions" : [".commentable_item ._sa_",".UFICommentActions"],
"likes counter" : [".UFILikeSentence"],
"shares counter" : [".UFIShareRow"],
"add comment form" : [".UFIAddComment"],
"date and privacy" : ["._5pcp"],
"Wall details column" : [".fbTimelineTwoColumn"],
"left column " : [".home_right_column",".pagelet"],
"side menu" : [".UFIAddComment"],
}, cssStr = '';
function setDefaults(){
localStorage.setItem('like and share actions', 1);
localStorage.setItem('shares counter', 1);
localStorage.setItem('likes counter', 1);
localStorage.setItem('add comment form', 1);
localStorage.setItem('date and privacy', 1);
localStorage.setItem('left column on feed page', 0);
localStorage.setItem('left column on group page', 0);
localStorage.setItem("Wall details column", 0);
localStorage.setItem("side menu", 0);
localStorage.setItem("date and privacy", 0);
setCssBySettings();
}
setDefaults();
var waitWithToggle;
chrome.runtime.onMessage.addListener(function (data, sender, callback) {
//console.log(data)
if("getSettings" == data.action ){
callback(getSettings());
}
if("setSettings" == data.action ){
for(var ind of Object.keys(data.settings)){
//console.log(ind, data.settings[ind]);
localStorage.setItem(ind, data.settings[ind]);
}
setCssBySettings();
callback(true);
}
if("toggleReader" == data.action ){
waitWithToggle = setTimeout(toggleReader, 1100);
setTimeout(function(){
localStorage.setItem('usedAlready',1);
},10000);
}
if("denayLastRequest" == data.action ){
//console.log(waitWithToggle,'denayLastRequest')
if(waitWithToggle){
clearTimeout(waitWithToggle);
waitWithToggle = null;
}
}
});
function toggleReader(){
localStorage.setItem('readerOn',Number(!getReaderState()));
injectAllTabs(true);
togglePageActionIcon();
waitWithToggle = null;
}
function togglePageActionIcon(){
// chrome.tabs.query( { active: true, currentWindow: true }, function(tabs){
// //console.log(tabs[0]);
// injectJsCurrentTab(tabs[0]);
// });
}
function getReaderState(){
return Number(localStorage.getItem('readerOn'));
}
function getSettings(){
var settings = {};
for(var ind of Object.keys(cssByElements)){
settings[ind] = Number(localStorage.getItem(ind));
}
return settings;
}
function setCssBySettings(){
var settings = getSettings();
css = [];
for(var ind of Object.keys(cssByElements)){
if(settings[ind]){
css.push(cssByElements[ind].join(", body.reader-on "));
}
}
cssStr = 'body.reader-on ' + css.join(', body.reader-on ') + "{display:none!important;}"
}
function injectToTab(tab, onClick){
//console.log(tab)
if(isFacebook(tab.url)){
//console.log('inject to ' , onClick)
if(tab.active && localStorage.getItem('usedAlready')){
//ensure tab opend
setTimeout(function(){
// injectJsCurrentTab();
},700);
}
if(getReaderState()){
chrome.pageAction.setIcon({tabId: tab.id,path: chrome.runtime.getURL('images/browseraction.png')});
chrome.pageAction.setTitle({tabId: tab.id,title:chrome.i18n.getMessage("stop_reader")});
chrome.tabs.insertCSS(tab.id, {code:cssStr});
chrome.tabs.executeScript(tab.id, {code:"document.body.classList.add('reader-on')"});
//add message just when page action clicked
if(tab.active && onClick){
chrome.tabs.executeScript(tab.id, {file:"js/on-toggle.js"}, function(){
chrome.tabs.executeScript(tab.id, {code:"chooseMessage(1)"});
});
}
}
else{
chrome.pageAction.setIcon({tabId: tab.id,path: chrome.runtime.getURL('images/browseraction_off.png')});
chrome.pageAction.setTitle({tabId: tab.id,title:chrome.i18n.getMessage("start_reader")});
chrome.tabs.executeScript(tab.id, {code:"document.body.classList.remove('reader-on')"});
//add message just when page action clicked
if(tab.active && onClick){
chrome.tabs.executeScript(tab.id, {file:"js/on-toggle.js"}, function(){
chrome.tabs.executeScript(tab.id, {code:"chooseMessage(0)"});
});
}
}
}
}
// chrome.pageAction.onClicked.addListener(function(tab){
// console.log(tab)
// chrome.pageAction.setPopup({
// tabId: tab.id,
// popup : '<html><body>sdsdsdfsdf</body></html>'
// });
// chrome.pageAction.getPopup(
// tab.id,
// function(str){
// console.log(str);
// }
// );
// })
function changePopup(tab){
console.log(tab.url);
//on facebook page, or on any pages if url null (permissions didnt granted)
if(isFacebook(tab.url) || !tab.url){
console.log(tab.id);
chrome.pageAction.show(tab.id);
}
}
function injectAllTabs(onClick){
chrome.tabs.query({currentWindow: false}, function(tabs) {
tabs.forEach(function(tab) {
//console.log(tab);
changePopup(tab);
injectToTab(tab, onClick);
});
});
chrome.tabs.query({currentWindow: true}, function(tabs) {
tabs.forEach(function(tab) {
changePopup(tab);
injectToTab(tab, onClick);
});
});
chrome.tabs.onCreated.addListener(function(tab){
injectToTab(tab, false);
changePopup(tab);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
console.log(tab);
injectToTab(tab, false);
changePopup(tab);
});
}
injectAllTabs();
function isFacebook(url){
return /https?:\/\/.+\.facebook.com/.test(url);
}