-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
104 lines (84 loc) · 3.39 KB
/
script.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
// utils
var forEach = Array.prototype.forEach;
chrome.extension.sendRequest ( { method: 'getData' }, function ( extensionData ) {
var usersToHighlight = extensionData.users;
var highlightBackgroundColour = extensionData.highlightBackgroundColour;
var highlightColour = extensionData.highlightColour;
var foundUsersEl = document.createElement ( 'div' );
foundUsersEl.id = 'HnFU-FoundUsers';
if ( location.href.indexOf ( 'user?id=' ) !== -1 ) {
var currentUser = location.href.substr ( location.href.indexOf ( 'user?id=' ) + 8 );
var removeEl = document.createElement ( 'a' );
removeEl.href = "#remove";
removeEl.id = 'HnFU-AddRemoveLink';
removeEl.innerHTML = '<img src="' + chrome.extension.getURL ( 'img/x.png' ) + '" /> remove from favourites';
foundUsersEl.appendChild ( removeEl );
var addEl = document.createElement ( 'a' );
addEl.href = "#add";
addEl.id = 'HnFU-AddRemoveLink';
addEl.innerHTML = '<img src="' + chrome.extension.getURL ( 'img/heart.png' ) + '" /> add to favourites';
foundUsersEl.appendChild ( addEl );
removeEl.onclick = function () {
chrome.extension.sendRequest ( { method: 'remove', userName: currentUser }, function () {
addEl.style.display = 'block';
removeEl.style.display = 'none';
} );
return false;
};
addEl.onclick = function () {
chrome.extension.sendRequest ( { method: 'add', userName: currentUser }, function () {
removeEl.style.display = 'block';
addEl.style.display = 'none';
} );
return false;
};
if ( usersToHighlight.indexOf ( currentUser ) !== -1 ) {
removeEl.style.display = 'block';
addEl.style.display = 'none';
} else {
addEl.style.display = 'block';
removeEl.style.display = 'none';
}
} else {
// get all user links on the page
var userLinks = document.querySelectorAll ( 'a[href^="user?id="]' );
var usersFound = [];
forEach.call ( userLinks, function ( userLink ) {
if ( userLink.parentNode && userLink.parentNode.className === 'pagetop' ) {
return;
}
if ( usersToHighlight.indexOf ( userLink.innerHTML ) !== -1 ) {
userLink.style.color = highlightColour;
userLink.style.backgroundColor = highlightBackgroundColour;
userLink.className = 'HnFU-User';
if ( usersFound.indexOf ( userLink.innerHTML ) === -1 ) {
usersFound.push ( userLink.innerHTML );
}
}
} );
if ( usersFound.length > 0 ) {
var foundUsersTitleEl = document.createElement ( 'h1' );
foundUsersTitleEl.innerHTML = 'Favourite users on this page:';
foundUsersEl.appendChild ( foundUsersTitleEl );
usersFound.forEach ( function ( userFound ) {
var tmpEl = document.createElement ( 'a' );
tmpEl.href = 'https://news.ycombinator.com/user?id=' + userFound;
tmpEl.innerHTML = userFound;
foundUsersEl.appendChild ( tmpEl );
} );
} else {
var noUsersFoundEl = document.createElement ( 'div' );
noUsersFoundEl.innerHTML = 'No favourite users found.';
foundUsersEl.appendChild ( noUsersFoundEl );
}
}
var optionsContainerEl = document.createElement ( 'div' );
optionsContainerEl.id = 'HnFU-OptionsLinkContainer';
foundUsersEl.appendChild ( optionsContainerEl );
var optionsLink = document.createElement ( 'a' );
optionsLink.href = chrome.extension.getURL ( 'options.html' );
optionsLink.title = 'HN Favourite Users - options';
optionsLink.id = 'HnFU-OptionsLink';
foundUsersEl.appendChild ( optionsLink );
document.body.appendChild ( foundUsersEl );
} );