-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
contentscript.js
63 lines (52 loc) · 2.2 KB
/
contentscript.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
//
// Script added to each page to check for twitter links
//
// await requests from parent extension
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
var accounts = [],
pageHtml;
// get the data we need from the page
if (message.action == 'getPageData') {
pageHtml = document.body.innerHTML;
// gather all twitter links in page
accounts = pageHtml.match(/\/\/(www\.)?twitter.com\/(([A-z0-9_-]{1,15}).*?)(\'|\"|\s)/g);
accounts2 = pageHtml.match(/\/\/(www|platform\.)?twitter.com\/(.*?)screen_name=(([A-z0-9_-]{1,15}).*?)(&|\'|\"|\s)/g);
accounts = [].concat(accounts, accounts2);
// any found? (and not on twitter itself)
if (accounts && ! location.href.match('twitter.com')) {
// clean up the urls to just the handles
accounts = accounts.filter(function (account) {
// Only strings please
return typeof account === 'string';
}).map(function(account) {
var matches = account.match(/\/\/(www\.)?twitter.com\/([A-z0-9_-]{1,15})/);
if (matches) {
return matches ? matches[2] : '';
}
matches = account.match(/\/\/(www|platform\.)?twitter.com\/(.*?)screen_name=([A-z0-9_-]{1,15})/);
return matches ? matches[3] : '';
}).filter(function (account) {
// remove non account keywords
return ['intent', 'statuses', 'share', 'home'].indexOf(account) === -1;
}).filter(function (account) {
// remove when its all numbers as thats just a status
return ! ( ! isNaN(parseFloat(account)) && isFinite(account));
}).filter(function(elem, pos, self) {
// remove duplicates or blanks
return self.indexOf(elem) == pos && elem !== '';
});
// respond back
sendResponse({
accounts: accounts
});
} else {
sendResponse({
accounts: []
});
}
}
// redirect?
if (message.action == 'setUrl') {
location.href = message.url;
}
});