forked from sowdust/searchbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·183 lines (159 loc) · 5.9 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
/**
* SearchBook
*
* @author sowdust<[email protected]>
* @url https://github.com/sowdust/searchbook
*
* License: free
*
*/
var query = window.localStorage.getItem('facebook-search-query');
var original_query = '';
var pattern = "https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet*";
//var query_regex = /%7B%5C%22bqf%5C%22%3A%5C%22(.+?)%5C%22%2C%5C%22browse_sid%5C%22/;
var query_regex = /encoded_query%22%3A%22(.+?)%22%2C%22/;
var cursor_regex = /cursor%22%3A%22(.+?)%22/;
var page_regex = /%22page_number%22%3A(\d+)%2C/;
var view_regex = /%7B%22view%22%3A%22([a-z]+)%22/;
var require_grid = ['photos','videos'];
var page_number = 1;
console.log('SearchBook Extension loaded');
function error(error) {
console.log(error);
}
function add_warning() {
var alert_code = "var div = document.createElement('div');";
alert_code += "div.id='warning_message';";
alert_code += "div.style.position = 'fixed';";
alert_code += "div.style.verticalAlign = 'middle';";
alert_code += "div.style.padding = '0 auto';";
alert_code += "div.style.textAlign = 'center';";
alert_code += "div.style.backgroundColor = '#f44336';";
alert_code += "div.style.color = 'white';";
alert_code += "div.style.fontWeight = 'bold';";
alert_code += "div.style.bottom = 0;";
alert_code += "div.style.width = '100%';";
alert_code += "div.style.zIndex = '999';";
alert_code += "div.innerHTML = 'Intercepting';";
alert_code += "document.body.appendChild(div);";
alert_code += "console.log('warning');";
alert_code += "var q = '" + query + "';";
alert_code += "var o = '" + original_query + "';";
alert_code += "var t = ' <tt>Intercepting requests</tt><br />';";
alert_code += "t += '<tt>Replacing with query: ' + q + '</tt><br />';";
alert_code += "div.innerHTML = t;";
browser.tabs.executeScript({ code: alert_code });
}
function remove_old_results() {
var clear_code = "console.log('trying to remove old results');";
clear_code += "var query=\"" + query + "\";";
clear_code += "var d = document.getElementById('pagelet_loader_u_ps_0_3_0_browse_result_below_fold');";
clear_code += "var e = document.getElementById('BrowseResultsContainer');";
clear_code += "var f = document.getElementById('bootstrap_entity_module');";
clear_code += "var g = document.getElementById('u_ps_jsonp_3_3_0_browse_result_below_fold');";
clear_code += "if(d) { d.innerHTML = '<h3 style=\"padding: 3px\">Results for ' + query + '</h3>'; }";
clear_code += "if(e) { e.innerHTML = ''; }";
clear_code += "if(f) { f.innerHTML = ''; }";
clear_code += "if(g) { g.innerHTML = ''; }";
browser.tabs.executeScript({ code: clear_code });
}
function stop_scrolling() {
console.log('stop_scrolling');
clearInterval(window.interval);
}
function scroll_to_bottom() {
var scroll_code ='window.scrollTo(0, document.body.scrollHeight);';
var stop_scroll_code = 'document.addEventListener("keydown", function (e) {';
stop_scroll_code += 'if (e.key == "Escape") { browser.runtime.sendMessage({ command: "stop_scrolling" }); }';
stop_scroll_code += '}, false);';
var html = document.documentElement.innerHTML;
console.log('setting interval');
window.interval = setInterval(function(){
// if ESC key is pressed, stop the scrolling
browser.tabs.executeScript({ code: stop_scroll_code });
html = document.documentElement.innerHTML;
if (html.indexOf('browse_end_of_results_footer') >= 0) {
console.log("Reached end of results");
clearInterval(window.interval);
}
browser.tabs.executeScript({ code: scroll_code });
console.log('setting interval 3');
},2000);
}
function redirect(requestDetails) {
if(!query) {
error('Query not set');
return;
}
modifiedUrl = requestDetails.url;
if(1 == page_number) {
// remove the cursor reference
var q = cursor_regex.exec(modifiedUrl);
if(q.length == 2) {
modifiedUrl = modifiedUrl.replace(q[1],'');
}
// remove results related to original query
remove_old_results();
}
// avoid infinite loops
if(modifiedUrl.endsWith('&modified=true')) {
console.log('Request already modified');
return;
} else {
modifiedUrl = modifiedUrl + '&modified=true';
}
// replace the original query with ours
var m = query_regex.exec(modifiedUrl);
if(m.length != 2) {
error('Query pattern matching error');
return;
}
original_query = m[1];
modifiedUrl = modifiedUrl.replace(m[1],encodeURI(query));
// replace page number
var p = page_regex.exec(modifiedUrl);
if(!p || p.length != 2) {
error('Page pattern matching error');
return;
}
modifiedUrl = modifiedUrl.replace('%22page_number%22%3A'+ p[1] +'%2C', '%22page_number%22%3A'+ page_number +'%2C');
page_number = page_number + 1;
// set the right view mode
for(var i=0; i<require_grid.length; ++i) {
if(query.includes(require_grid[i])) {
var v = view_regex.exec(modifiedUrl);
if(v.length == 2) {
modifiedUrl = modifiedUrl.replace('%7B%22view%22%3A%22'+ v[1] +'%22', '%7B%22view%22%3A%22'+ 'grid' +'%22');
}
}
}
// warn the user we are intercepting
if(!document.getElementById('warning_message')) {
add_warning();
}
return {
redirectUrl: modifiedUrl
};
}
// set listener for redirection
browser.webRequest.onBeforeRequest.addListener(
redirect,
{urls:[pattern]},
["blocking"]
);
// set listener for messages from the popup interface
browser.runtime.onMessage.addListener((message) => {
if (message.command === "set_query") {
console.log('Setting query to ' + message.query);
query = message.query;
page_number = 1;
} else if (message.command === "clear_query") {
console.log('Removing query');
query = '';
} else if (message.command === "scroll_to_bottom") {
console.log('Scrolling');
scroll_to_bottom();
} else if (message.command === "stop_scrolling") {
stop_scrolling();
}
});