-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
95 lines (84 loc) · 2.7 KB
/
content.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
/**
* content.js can access dom elements and manipulate them whereas background.js cannot.
*
* As such, content.js is able to get the username with jquery without any highlighting needed.
*
* @author Thomas Ortiz
*/
/**
* When the document is ready run
*/
$(function () {
generateAlbumSpan();
checkIfOnUserPage();
});
/**
* Creates an album link underneath the header.
*
* It removes any previous headers that were there (since Imgur doesn't reload pages)
* and then adds the new album link in its place.
*/
var generateAlbumSpan = function(){
var username = getUserName();
$("#albumSpan").remove();
$("div.post-title-meta.font-opensans-semibold").append("<span id='albumSpan'><a href='http://" + username + ".imgur.com'>Albums</a></span>");
}
/**
* Checks if we are on a user gallery page.
*
* If we are it adds a Albums link.
*/
var checkIfOnUserPage = function(){
if( currentPage.match("/[^%]imgur.com\/user\/*/") ){
var username = getUserNameFromUrl();
$(".right").prepend("<div class='panel menu'><div class='textbox button'><a href='http://" + username + ".imgur.com'><h2>Albums</h2></a></div></div>")
}
}
/**
* This listens for changes in the url and will regenerate the album link when needed.
*/
var currentPage = window.location.href;
setInterval(function(){
// Check if the page has changed and we need to update information
if (currentPage != window.location.href){
currentPage = window.location.href;
generateAlbumSpan();
}
}, 500);
/**
* Retrieves the username from the URL
*
* @return username
*/
var getUserNameFromUrl = function(){
var url = window.location.href;
username = url.split('user/')[1]; // gets <username></potentially junk>
username = username.split('/')[0]; // gets <username>'
return username;
}
// Get the username
var getUserName = function () {
// Get the username of the person that create the picture in the gallery
var imgurUserName = $(".post-title-meta.font-opensans-semibold a").html();
// Default to the current user's albums
if(!imgurUserName){
imgurUserName = $('a[href^="//imgur.com/user"]').html()
}
return imgurUserName;
};
// Get message from background.js to retrieve the username.
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
// Get the username and return it as a response
if (message.functiontoInvoke == "getUserName"){
var username = getUserName();
sendResponse({
response: username
});
}
if (message.functiontoInvoke == "getUserNameFromUrl"){
var username = getUserNameFromUrls();
sendResponse({
response: username
});
}
});