-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopup.js
50 lines (42 loc) · 1.87 KB
/
popup.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
//
// Popup script which gets the accounts list back from the main extension
//
// request the accounts found
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'getPageData' }, (response) => {
var container = document.getElementById('twitter-accounts-container'),
template = document.getElementById('template-twitter-account').innerHTML,
html = '',
scriptTag,
firstScriptTag,
links;
// no response?
if (! response || ! response.accounts) {
container.innerHTML = '<p>Unable to get twitter accounts from this page.</p>';
return;
}
// no accounts found?
if (! response.accounts.length) {
container.innerHTML = '<p>No twitter accounts found on this page.</p>';
return;
}
// Build the html list of accounts found
response.accounts.forEach(function (account) {
html += template.replace(/\{account\}/g, account);
});
container.innerHTML = html;
// link to twitter
scriptTag = document.createElement('script');
firstScriptTag = document.getElementsByTagName('script')[0];
scriptTag.src = 'twitter-widgets.js'; // local copy of http://platform.twitter.com/widgets.js
firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag);
// setup twitter profile links to actually link over
links = document.getElementsByClassName('twitter-profile-link');
[].forEach.call(links, function (link) {
link.addEventListener('click', function () {
// send a message back to the contentscript to redirect the page
chrome.tabs.sendMessage(tabs[0].id, { action: 'setUrl', url: link.href });
}, false);
});
});
});