Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/public/js/main.js
kilo 43fd3e8956 Sync JS cookies & Go cookies's domain & random other things (#1571)
* create JS domain variable

* use domain JS variable when creating cookies

* Create getDomainName() function in Go

* Update template_functions_test.go

* Update template_functions_test.go

* recreate cookie when resetCookies() is called

* Update main.css

* Update classic.css

* show "unknown" instead of "0 0 0" if stats are unknown

* don't show top ad if there's an announcement

* remove BBCode support that broke description's line breaks

* remove useless identifier

* hide error instead of fixing it

* Update markdown_test.go

* Update markdown_test.go

* css for tags in classic

* remove wrong explanation for alternating color

* fix RSS URL in header

* remove eu cookie <div> from base

* fix cookie warning never showing up

* Update main.js

* nyaa.se-like styling for upload form on classic.css

* Smaller markdown toolbar on classic upload form

* Fix indent, non-round borders for markdown & language list on classic

* Update view.jet.html

* add fillZero function

* ditto but in test

* Update view.jet.html

* add RJ prefix to dlsite link

* Update template_functions_test.go

* Update listing.jet.html

* prevent responsiveness of upload form in classic.css

* remove now useless css rule

* fix tag input padding on classic

* Update template_functions_test.go

* rollback workaround

* rollback workaround

* rollback workaround

* remove now unused function

* ditto

* Update main.css
2017-09-17 22:10:43 +02:00

248 lignes
9,6 Kio
JavaScript

// @source https://github.com/NyaaPantsu/nyaa/tree/dev/public/js
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
//String that will contain a far future date, used multiple times throughout multiple functions
var farFutureString
//Array that will contain the themes that the user will switch between when triggering the function a few lines under
var UserTheme
// Switches between themes when a new one is selected
function switchThemes() {
var themeName = document.getElementById("theme-selector").value
var head = document.getElementsByTagName("head")[0]
if (themeName === "") {
return
}
// Remove the theme in place, it fails if one isn't set
try {
head.removeChild(document.getElementById("theme"))
} catch (err) {}
// Don't add a node if we don't want extra styling
// Create the new one and put it back
var newTheme = document.createElement("link")
newTheme.setAttribute("rel", "stylesheet")
newTheme.setAttribute("href", "/css/" + themeName + ".css")
newTheme.setAttribute("id", "theme")
head.appendChild(newTheme)
}
// Used by spoiler tags
function toggleLayer(elem) {
if (elem.classList.contains("hide")) {
elem.classList.remove("hide")
} else {
elem.classList.add("hide")
}
}
function parseAllDates() {
// Date formatting
var lang = document.getElementsByTagName("html")[0].getAttribute("lang")
var ymdOpt = {
year: "numeric",
month: "short",
day: "numeric"
}
var hmOpt = {
hour: "numeric",
minute: "numeric"
}
var list = document.getElementsByClassName("date-short")
for (var i in list) {
var e = list[i]
e.title = new Date(e.innerText).toLocaleString(lang)
e.innerText = new Date(e.innerText).toLocaleString(lang, ymdOpt)
}
var list = document.getElementsByClassName("date-full")
for (var i in list) {
if(list.length == 0)
break;
var e = list[i]
var dateDifference = dateDiff(new Date(e.innerText), new Date())
if(e.className != undefined && e.className.includes("scrape-date"))
e.title = ((dateDifference.d * 24) + dateDifference.h) + " hours " + dateDifference.m + " minutes ago"
else
e.title = dateDifference.d + " days " + dateDifference.h + " hours ago"
e.innerText = new Date(e.innerText).toLocaleString(lang)
}
}
function dateDiff( str1, str2 ) {
var diff = Date.parse( str2 ) - Date.parse( str1 );
return isNaN( diff ) ? NaN : {
diff : diff,
m : Math.floor( diff / 60000 % 60 ),
h : Math.floor( diff / 3600000 % 24 ),
d : Math.floor( diff / 86400000 )
};
}
parseAllDates()
//called if no Commit cookie is set or if the website has a newer commit than the one in cookie
function resetCookies() {
var cookies = document.cookie.split(";")
var excludedCookies = ["mascot", "theme", "theme2", "mascot_url", "lang", "csrf_token", "altColors", "EU_Cookie", "oldNav"]
//Remove all cookies but exclude those in the above array
for (var i = 0; i < cookies.length; i++) {
var cookieName = (cookies[i].split("=")[0]).trim()
//Trim spaces because some cookie names have them at times
if (excludedCookies.includes(cookieName)) {
if(domain == "pantsu.cat") {
//only execute if cookie are supposed to be shared between nyaa & sukebei
var cookieValue = getCookieValue(cookieName)
document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
document.cookie = cookieName + "=" + cookieValue + ";expires=" + farFutureString + ";domain=" + domain
//Remove cookie and re-create it to ensure domain is correct
}
continue
}
document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
}
//Set new version in cookie
document.cookie = "commit=" + commitVersion + ";expires=" + farFutureString + ";domain=" + domain
var oneHour = new Date()
oneHour.setTime(oneHour.getTime() + 1 * 3600 * 1500)
document.cookie = "newVersion=true; expires=" + oneHour.toUTCString() + ";domain=" + domain
}
/*Fixed-Navbar offset fix*/
if (document.getElementsByClassName("search-box")[0] !== undefined)
startupCode()
else
document.addEventListener("DOMContentLoaded", function (event) {
startupCode()
})
function startupCode() {
farFutureString = new Date()
farFutureString.setTime(farFutureString.getTime() + 50 * 36000 * 15000)
farFutureString = farFutureString.toUTCString()
var shiftWindow = function () {
scrollBy(0, -70)
}
if (location.hash) shiftWindow()
window.addEventListener("hashchange", shiftWindow)
if (!document.cookie.includes("commit"))
resetCookies()
else {
var userCommitVersion = getCookieValue("commit");
//Get start and end position of Commit string, need to start searching endPos from version cookie in case it's not the first cookie in the string
//If endPos is equal to -1, aka if the version cookie is at the very end of the string and doesn't have an ";", the endPos is not used
if (userCommitVersion != commitVersion)
resetCookies()
}
if(document.getElementById("cookie-warning-close") != null) {
document.getElementById("cookie-warning-close").addEventListener("click", function (e) {
document.getElementById("cookie-warning").outerHTML = "";
})
}
if (document.cookie.includes("newVersion"))
document.getElementById("commit").className = document.getElementById("commit").innerHTML != "unknown" ? "new" : "wew";
document.getElementById("dark-toggle").addEventListener("click", toggleTheme);
if(document.cookie.includes("theme=")) {
UserTheme = [getCookieValue("theme"), "tomorrow"]
//Get user's default theme and set the alternative one as tomorrow
}
else
UserTheme = ["g", "tomorrow"]
//If user has no default theme, set these by default
if(document.cookie.includes("theme2=")) {
UserTheme[1] = getCookieValue("theme2")
//If user already has ran the ToggleTheme() function in the past, we get the value of the second theme (the one the script switches to)
if(!UserTheme.includes("tomorrow"))
UserTheme[1] = "tomorrow"
//If none of the theme are tomorrow, which happens if the user is on dark mode (with theme2 on g.css) and that he switches to classic or g.css in settings, we set the second one as tomorrow
else if(UserTheme[0] == UserTheme[1])
UserTheme[1] = "g"
//If both theme are tomorrow, which happens if theme2 is on tomorrow (always is by default) and that the user sets tomorrow as his theme through settings page, we set secondary theme to g.css
}
else {
if(UserTheme[0] == UserTheme[1])
UserTheme[1] = "g"
//If tomorrow is twice in UserTheme, which happens when the user already has tomorrow as his default theme and toggle the dark mode for the first time, we set the second theme as g.css
document.cookie = "theme2=" + UserTheme[1] + ";path=/;expires=" + farFutureString + ";domain=" + domain
//Set cookie for future theme2 uses
}
}
function toggleTheme(e) {
var CurrentTheme = document.getElementById("theme").href
CurrentTheme = CurrentTheme.substring(CurrentTheme.indexOf("/css/") + 5, CurrentTheme.indexOf(".css"))
CurrentTheme = (CurrentTheme == UserTheme[0] ? UserTheme[1] : UserTheme[0])
document.getElementById("theme").href = "/css/" + CurrentTheme + ".css";
if(UserID > 0 ){
Query.Get("/dark", function(data) {})
//If user logged in, we're forced to go through this page in order to save the new user theme
}
else {
document.cookie = "theme=" + CurrentTheme + ";path=/;expires=" + farFutureString + ";domain=" + domain
document.cookie = "theme2=" + (CurrentTheme == UserTheme[0] ? UserTheme[1] : UserTheme[0]) + ";path=/;expires=" + farFutureString + ";domain=" + domain
//Otherwise, we can just set the theme through cookies
}
e.preventDefault()
}
function playVoice() {
var mascotAudio = document.getElementById("explosion") || document.getElementById("nyanpassu") || document.getElementById("nyanpassu2") || document.getElementById("kawaii")
if (mascotAudio !== undefined) {
mascotAudio.volume = 0.2
mascotAudio.play()
} else {
console.log("Your mascot doesn't support yet audio files!")
}
}
document.getElementsByClassName("form-input refine")[0].addEventListener("click", function (e) {
if(document.getElementsByClassName("form-input search-box")[0].value == "" || location.pathname != "/")
{
document.getElementsByClassName("box refine")[0].style.display = document.getElementsByClassName("box refine")[0].style.display == "none" ? "block" : "none"
if (document.getElementsByClassName("form-input refine-searchbox")[0].value == "")
document.getElementsByClassName("form-input refine-searchbox")[0].value = document.getElementsByClassName("form-input search-box")[0].value
if (document.getElementsByClassName("form-input refine-category")[0].selectedIndex == 0)
document.getElementsByClassName("form-input refine-category")[0].selectedIndex = document.getElementsByClassName("form-input form-category")[0].selectedIndex
if (document.getElementsByClassName("box refine")[0].style.display == "block")
scrollTo(0, 0)
e.preventDefault()
}
})
function humanFileSize(bytes, si) {
if (bytes == 0)
return "Unknown"
var k = si ? 1000 : 1024
var i = ~~(Math.log(bytes) / Math.log(k))
return i == 0 ? bytes + " B" : (bytes / Math.pow(k, i)).toFixed(1) + " " + "KMGTPEZY" [i - 1] + (si ? "" : "i") + "B"
}
function getCookieValue(cookieName) {
var startPos = document.cookie.indexOf(cookieName + "=") + cookieName.length + 1
var endPos = document.cookie.substring(startPos).indexOf(";")
return endPos == "-1" ? document.cookie.substring(startPos) : document.cookie.substring(startPos, endPos + startPos)
}
// @license-end