forked from NationStates/NationStatesPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nationstates.js
91 lines (86 loc) · 2.85 KB
/
nationstates.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
var quote = '<button id="quote-btn-${id}" class="button QuoteButton" onclick="quotePost(this);">Quote</button>';
(function() {
window.postMessage({ method: "unread_forum_posts"}, "*");
checkPanelAlerts();
addCustomAlerts();
checkPageHappenings()
})();
function addCustomAlerts() {
if (localStorage.getItem("show_admin_area") == "true") {
// $(".menu").append("<li><a id='nationbot' href='http://capitalistparadise.com/api/nationbot/'>NATIONBOT ONLINE</a></li>");
}
}
function checkPageHappenings() {
if (getVisiblePage() == "un" || getVisiblePage() == "nation") {
$.get(window.location.href, function(page) {
var happeningSelector;
if (getVisiblePage() == "un") {
happeningSelector = $(page).find("h3:contains('Recent Events')").next();
} else {
happeningSelector = $(page).find(".newsbox").find("ul");
}
$(happeningSelector.children().get().reverse()).each(function() {
var html = $(this).html();
var split = $(this).text().split(":");
var found = false;
var happenings = (getVisiblePage() == "un" ? $("h3:contains('Recent Events')").next() : $(".newsbox").find("ul"));
$(happenings.children()).each(function() {
if ($(this).text().contains(split[1])) {
$(this).html(html);
found = true;
return false;
}
});
if (!found) {
happenings.prepend("<li>" + html + "</li>");
}
});
});
setTimeout(checkPageHappenings, isPageActive() ? 6000 : 30000);
}
}
var _lastPanelUpdate = 0;
function checkPanelAlerts() {
//console.log("Checking Panel Alerts: " + Date.now());
setTimeout(function() {
var updateDelay = 10000; //10 sec
if (!isPageActive()) {
updateDelay = 300000; //5 min
} else if (getLastActivity() + 60000 < Date.now()) {
updateDelay = 150000; //2.5 min
}
if (Date.now() > (_lastPanelUpdate + updateDelay)) {
_lastPanelUpdate = Date.now();
updatePanelAlerts();
}
checkPanelAlerts();
}, 10000);
}
window.addEventListener("message", function(event) {
if (event.data.method == "unread_forum_posts_amt") {
console.log("Unread forum posts: " + event.data.amt);
if (event.data.amt != _unreadForumPosts) {
_unreadForumPosts = event.data.amt;
$("#panel").find("a").each(function() {
if ($(this).html().indexOf("FORUM") != -1) {
$(this).html("FORUM (" + _unreadForumPosts + ")");
return false;
}
});
}
}
}, false);
var _panelForumLink = null;
var _unreadForumPosts = 0;
function updatePanelAlerts() {
var unread = 0;
if (getUserNation() != "") {
window.postMessage({ method: "unread_forum_posts"}, "*");
$.get('/page=panel/template-overall=none', function(html) {
var searchString = '<a href="page=telegrams">';
var indicatorStart = html.indexOf(searchString);
var indicatorEnd = html.indexOf('</a>', indicatorStart);
$('a[href$="page=telegrams"]').html(html.substring(indicatorStart + searchString.length, indicatorEnd));
});
}
}