2017-06-04 07:43:41 +02:00
// @source https://github.com/NyaaPantsu/nyaa/tree/dev/public/js
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
2017-05-26 13:13:06 +02:00
2017-08-22 03:34:20 +02:00
//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
2017-07-22 23:27:02 +02:00
2017-10-15 07:31:05 +02:00
var Mirror = false
2017-08-22 03:34:20 +02:00
// Switches between themes when a new one is selected
2017-07-24 02:32:50 +02:00
function switchThemes ( ) {
2017-07-24 07:03:43 +02:00
var themeName = document . getElementById ( "theme-selector" ) . value
2017-07-21 01:47:36 +02:00
var head = document . getElementsByTagName ( "head" ) [ 0 ]
2017-08-17 03:35:18 +02:00
if ( themeName === "" ) {
return
}
2017-07-16 17:36:17 +02:00
// Remove the theme in place, it fails if one isn't set
2017-07-24 02:32:50 +02:00
try {
2017-07-21 01:47:36 +02:00
head . removeChild ( document . getElementById ( "theme" ) )
2017-07-24 02:32:50 +02:00
} catch ( err ) { }
2017-07-16 17:36:17 +02:00
// Don't add a node if we don't want extra styling
2017-08-17 03:35:18 +02:00
2017-07-16 17:36:17 +02:00
// Create the new one and put it back
2017-07-21 01:47:36 +02:00
var newTheme = document . createElement ( "link" )
newTheme . setAttribute ( "rel" , "stylesheet" )
2017-10-04 15:46:06 +02:00
newTheme . setAttribute ( "href" , "/css/themes/" + themeName + ".css" )
2017-07-21 01:47:36 +02:00
newTheme . setAttribute ( "id" , "theme" )
head . appendChild ( newTheme )
2017-05-27 19:08:47 +02:00
}
2017-05-05 12:59:56 +02:00
// Used by spoiler tags
function toggleLayer ( elem ) {
2017-07-16 17:36:17 +02:00
if ( elem . classList . contains ( "hide" ) ) {
2017-07-21 01:47:36 +02:00
elem . classList . remove ( "hide" )
2017-07-16 17:36:17 +02:00
} else {
2017-07-21 01:47:36 +02:00
elem . classList . add ( "hide" )
2017-07-16 17:36:17 +02:00
}
2017-05-05 12:59:56 +02:00
}
2017-07-24 02:32:50 +02:00
2017-05-30 00:28:21 +02:00
function parseAllDates ( ) {
2017-07-16 17:36:17 +02:00
// Date formatting
2017-07-21 01:47:36 +02:00
var lang = document . getElementsByTagName ( "html" ) [ 0 ] . getAttribute ( "lang" )
2017-07-24 02:32:50 +02:00
var ymdOpt = {
year : "numeric" ,
month : "short" ,
day : "numeric"
}
var hmOpt = {
hour : "numeric" ,
minute : "numeric"
}
2017-05-05 15:55:25 +02:00
2017-07-21 01:47:36 +02:00
var list = document . getElementsByClassName ( "date-short" )
2017-10-11 03:24:09 +02:00
for ( var i = 0 ; i < list . length ; i ++ ) {
2017-07-21 01:47:36 +02:00
var e = list [ i ]
2017-10-11 03:24:09 +02:00
e . innerText = new Date ( e . title ) . toLocaleString ( lang , ymdOpt )
e . title = new Date ( e . title ) . toLocaleString ( lang )
2017-07-16 17:36:17 +02:00
}
2017-05-05 15:55:25 +02:00
2017-07-21 01:47:36 +02:00
var list = document . getElementsByClassName ( "date-full" )
2017-10-11 03:24:09 +02:00
for ( var i = 0 ; i < list . length ; i ++ ) {
2017-07-21 01:47:36 +02:00
var e = list [ i ]
2017-07-28 05:47:26 +02:00
var dateDifference = dateDiff ( new Date ( e . innerText ) , new Date ( ) )
2017-08-03 22:09:06 +02:00
2017-08-07 06:51:25 +02:00
if ( e . className != undefined && e . className . includes ( "scrape-date" ) )
e . title = ( ( dateDifference . d * 24 ) + dateDifference . h ) + " hours " + dateDifference . m + " minutes ago"
2017-08-03 22:09:06 +02:00
else
e . title = dateDifference . d + " days " + dateDifference . h + " hours ago"
2017-07-21 01:47:36 +02:00
e . innerText = new Date ( e . innerText ) . toLocaleString ( lang )
2017-07-16 17:36:17 +02:00
}
2017-05-05 15:55:25 +02:00
}
2017-07-26 10:59:48 +02:00
function dateDiff ( str1 , str2 ) {
var diff = Date . parse ( str2 ) - Date . parse ( str1 ) ;
return isNaN ( diff ) ? NaN : {
diff : diff ,
2017-08-03 22:09:06 +02:00
m : Math . floor ( diff / 60000 % 60 ) ,
2017-08-22 03:34:20 +02:00
h : Math . floor ( diff / 3600000 % 24 ) ,
d : Math . floor ( diff / 86400000 )
2017-07-26 10:59:48 +02:00
} ;
}
2017-07-21 01:47:36 +02:00
parseAllDates ( )
2017-05-31 04:21:35 +02:00
2017-07-24 02:32:50 +02:00
//called if no Commit cookie is set or if the website has a newer commit than the one in cookie
function resetCookies ( ) {
2017-07-28 05:47:26 +02:00
var cookies = document . cookie . split ( ";" )
2017-10-02 20:49:46 +02:00
var excludedCookies = [ "session" , "mascot" , "version" , "theme" , "theme2" , "mascot_url" , "lang" , "csrf_token" , "altColors" , "EU_Cookie" , "oldNav" ]
2017-09-21 06:18:55 +02:00
//Excluded cookies are either left untouched or deleted then re-created
//Ignored Cookies are constantly left untouched
//Get HostName without subDomain
var hostName = window . location . host
2017-09-22 06:54:19 +02:00
2017-10-15 07:31:05 +02:00
if ( ! Mirror ) {
var lastDotIndex = hostName . lastIndexOf ( "." )
var secondLast = - 1
for ( var index = 0 ; index < lastDotIndex ; index ++ ) {
if ( hostName [ index ] == '.' )
secondLast = index
}
hostName = hostName . substr ( secondLast == - 1 ? 0 : secondLast )
2017-09-21 06:18:55 +02:00
}
2017-07-23 04:50:36 +02:00
2017-07-24 02:32:50 +02:00
for ( var i = 0 ; i < cookies . length ; i ++ ) {
2017-07-28 05:47:26 +02:00
var cookieName = ( cookies [ i ] . split ( "=" ) [ 0 ] ) . trim ( )
2017-09-17 22:10:43 +02:00
//Trim spaces because some cookie names have them at times
if ( excludedCookies . includes ( cookieName ) ) {
2017-09-21 06:18:55 +02:00
if ( domain == hostName ) {
//only execute if cookie are supposed to be shared between nyaa & sukebei, aka on host name without subdomain
2017-09-17 22:10:43 +02:00
var cookieValue = getCookieValue ( cookieName )
document . cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
2017-10-02 17:59:35 +02:00
document . cookie = cookieName + "=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
2017-10-04 17:45:31 +02:00
if ( cookieName != "session" )
2017-10-02 20:49:46 +02:00
document . cookie = cookieName + "=" + cookieValue + ";path=/;expires=" + farFutureString + ";domain=" + domain
else document . cookie = cookieName + "=" + cookieValue + ";path=/;expires=" + farFutureString
2017-10-02 17:59:35 +02:00
//Remove cookie from both current & general path, then re-create it to ensure domain is correct
2017-10-02 20:49:46 +02:00
//Force current domain for session cookie
2017-09-17 22:10:43 +02:00
}
continue
}
2017-10-02 17:59:35 +02:00
document . cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
2017-09-18 06:20:25 +02:00
document . cookie = cookieName + "=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;"
2017-07-22 23:27:02 +02:00
}
2017-07-23 04:50:36 +02:00
2017-07-24 02:32:50 +02:00
//Set new version in cookie
2017-09-30 16:05:09 +02:00
document . cookie = "commit=" + commitVersion + ";path=/;expires=" + farFutureString + ";domain=" + domain
document . cookie = "version=" + websiteVersion + ";path=/;expires=" + farFutureString + ";domain=" + domain
2017-07-23 04:50:36 +02:00
2017-07-28 05:47:26 +02:00
var oneHour = new Date ( )
oneHour . setTime ( oneHour . getTime ( ) + 1 * 3600 * 1500 )
2017-09-30 16:05:09 +02:00
document . cookie = "newVersion=true;path=/;expires=" + oneHour . toUTCString ( ) + ";domain=" + domain
2017-07-24 02:32:50 +02:00
}
2017-07-23 04:50:36 +02:00
2017-07-22 23:27:02 +02:00
2017-05-13 00:37:37 +02:00
/*Fixed-Navbar offset fix*/
2017-07-24 02:32:50 +02:00
if ( document . getElementsByClassName ( "search-box" ) [ 0 ] !== undefined )
2017-07-23 04:50:36 +02:00
startupCode ( )
else
2017-07-24 02:32:50 +02:00
document . addEventListener ( "DOMContentLoaded" , function ( event ) {
startupCode ( )
} )
2017-07-16 17:36:17 +02:00
2017-07-23 00:00:44 +02:00
function startupCode ( ) {
2017-08-22 03:34:20 +02:00
farFutureString = new Date ( )
farFutureString . setTime ( farFutureString . getTime ( ) + 50 * 36000 * 15000 )
farFutureString = farFutureString . toUTCString ( )
2017-07-24 02:32:50 +02:00
var shiftWindow = function ( ) {
scrollBy ( 0 , - 70 )
}
2017-07-23 02:22:33 +02:00
if ( location . hash ) shiftWindow ( )
window . addEventListener ( "hashchange" , shiftWindow )
2017-07-23 00:00:44 +02:00
2017-10-15 07:31:05 +02:00
if ( ! window . location . host . includes ( domain ) ) {
domain = window . location . host
Mirror = true
}
2017-09-18 06:20:25 +02:00
if ( ! document . cookie . includes ( "commit" ) && ! document . cookie . includes ( "version" ) )
2017-07-23 19:08:42 +02:00
resetCookies ( )
else {
2017-09-18 06:20:25 +02:00
var userCommitVersion = getCookieValue ( "commit" ) , userWebsiteVersion = getCookieValue ( "version" ) ;
if ( userCommitVersion != commitVersion || userWebsiteVersion != websiteVersion )
2017-07-24 02:32:50 +02:00
resetCookies ( )
2017-07-23 19:08:42 +02:00
}
2017-09-06 10:51:25 +02:00
if ( document . getElementById ( "cookie-warning-close" ) != null ) {
document . getElementById ( "cookie-warning-close" ) . addEventListener ( "click" , function ( e ) {
document . getElementById ( "cookie-warning" ) . outerHTML = "" ;
2017-10-06 16:33:22 +02:00
document . cookie = "EU_Cookie=true;path=/;expires=" + farFutureString + ";domain=" + domain
2017-09-06 10:51:25 +02:00
} )
}
2017-07-23 19:08:42 +02:00
2017-07-24 02:32:50 +02:00
if ( document . cookie . includes ( "newVersion" ) )
2017-08-01 00:57:31 +02:00
document . getElementById ( "commit" ) . className = document . getElementById ( "commit" ) . innerHTML != "unknown" ? "new" : "wew" ;
document . getElementById ( "dark-toggle" ) . addEventListener ( "click" , toggleTheme ) ;
2017-08-02 14:01:02 +02:00
2017-08-26 04:15:28 +02:00
if ( document . cookie . includes ( "theme=" ) ) {
2017-09-22 06:54:19 +02:00
UserTheme = [ getCookieValue ( "theme" ) , darkTheme ]
//Get user's default theme and set the alternative one as dark theme
2017-08-02 14:01:02 +02:00
}
else
2017-09-22 06:54:19 +02:00
UserTheme = [ "g" , darkTheme ]
2017-08-02 14:01:02 +02:00
//If user has no default theme, set these by default
2017-08-26 04:15:28 +02:00
if ( document . cookie . includes ( "theme2=" ) ) {
2017-09-17 22:10:43 +02:00
UserTheme [ 1 ] = getCookieValue ( "theme2" )
2017-08-12 08:52:54 +02:00
//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)
2017-09-22 06:54:19 +02:00
if ( ! UserTheme . includes ( darkTheme ) )
UserTheme [ 1 ] = darkTheme
//If none of the theme are darkTheme, 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 darkTheme
2017-08-12 08:52:54 +02:00
else if ( UserTheme [ 0 ] == UserTheme [ 1 ] )
UserTheme [ 1 ] = "g"
2017-09-22 06:54:19 +02:00
//If both theme are darkTheme, which happens if theme2 is on darkTheme (always is by default) and that the user sets darkTheme as his theme through settings page, we set secondary theme to g.css
2017-08-02 14:01:02 +02:00
}
2017-09-30 14:50:54 +02:00
else if ( UserTheme [ 0 ] == UserTheme [ 1 ] )
UserTheme [ 1 ] = "g"
2017-09-22 06:54:19 +02:00
//If darkTheme is twice in UserTheme, which happens when the user already has darkTheme as his default theme and toggle the dark mode for the first time, we set the second theme as g.css
2017-08-02 14:01:02 +02:00
2017-08-01 00:57:31 +02:00
}
2017-08-02 19:34:53 +02:00
function toggleTheme ( e ) {
2017-08-01 00:57:31 +02:00
var CurrentTheme = document . getElementById ( "theme" ) . href
2017-10-04 17:45:31 +02:00
CurrentTheme = CurrentTheme . substring ( CurrentTheme . indexOf ( "/themes/" ) + 8 , CurrentTheme . indexOf ( ".css" ) )
2017-08-01 00:57:31 +02:00
CurrentTheme = ( CurrentTheme == UserTheme [ 0 ] ? UserTheme [ 1 ] : UserTheme [ 0 ] )
2017-10-04 15:46:06 +02:00
document . getElementById ( "theme" ) . href = "/css/themes/" + CurrentTheme + ".css" ;
2017-08-01 00:57:31 +02:00
2017-08-29 13:15:37 +02:00
if ( UserID > 0 ) {
2017-09-03 08:12:38 +02:00
Query . Get ( "/dark" , function ( data ) { } )
2017-08-29 13:15:37 +02:00
//If user logged in, we're forced to go through this page in order to save the new user theme
}
else {
2017-09-17 22:10:43 +02:00
document . cookie = "theme=" + CurrentTheme + ";path=/;expires=" + farFutureString + ";domain=" + domain
document . cookie = "theme2=" + ( CurrentTheme == UserTheme [ 0 ] ? UserTheme [ 1 ] : UserTheme [ 0 ] ) + ";path=/;expires=" + farFutureString + ";domain=" + domain
2017-08-29 13:15:37 +02:00
//Otherwise, we can just set the theme through cookies
}
2017-08-02 19:34:53 +02:00
e . preventDefault ( )
2017-07-23 00:00:44 +02:00
}
2017-05-26 13:13:06 +02:00
function playVoice ( ) {
2017-07-24 02:32:50 +02:00
var mascotAudio = document . getElementById ( "explosion" ) || document . getElementById ( "nyanpassu" ) || document . getElementById ( "nyanpassu2" ) || document . getElementById ( "kawaii" )
2017-07-16 17:36:17 +02:00
if ( mascotAudio !== undefined ) {
2017-07-21 01:47:36 +02:00
mascotAudio . volume = 0.2
mascotAudio . play ( )
2017-07-16 17:36:17 +02:00
} else {
console . log ( "Your mascot doesn't support yet audio files!" )
}
2017-05-29 13:12:05 +02:00
}
2017-07-10 05:57:59 +02:00
2017-07-10 15:24:52 +02:00
document . getElementsByClassName ( "form-input refine" ) [ 0 ] . addEventListener ( "click" , function ( e ) {
2017-07-30 04:52:16 +02:00
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"
2017-08-22 03:34:20 +02:00
if ( document . getElementsByClassName ( "form-input refine-searchbox" ) [ 0 ] . value == "" )
2017-07-30 04:52:16 +02:00
document . getElementsByClassName ( "form-input refine-searchbox" ) [ 0 ] . value = document . getElementsByClassName ( "form-input search-box" ) [ 0 ] . value
2017-08-22 03:34:20 +02:00
if ( document . getElementsByClassName ( "form-input refine-category" ) [ 0 ] . selectedIndex == 0 )
2017-07-30 04:52:16 +02:00
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 ( )
}
2017-07-21 01:47:36 +02:00
} )
2017-07-24 02:32:50 +02:00
2017-09-19 02:00:30 +02:00
document . getElementsByClassName ( "form-input refine-btn" ) [ 0 ] . addEventListener ( "click" , function ( e ) {
var inputs = document . querySelectorAll ( ".box.refine form input" )
var select = document . querySelectorAll ( ".box.refine form select" )
for ( var i = 0 ; i < inputs . length ; i ++ )
if ( inputs [ i ] . value == "" ) inputs [ i ] . disabled = true ;
for ( var i = 0 ; i < select . length ; i ++ )
if ( select [ i ] . selectedIndex == 0 ) select [ i ] . disabled = true ;
if ( document . querySelector ( ".box.refine form input[name='limit']" ) . value == "50" )
document . querySelector ( ".box.refine form input[name='limit']" ) . disabled = true
if ( document . querySelector ( ".box.refine form select[name='sort']" ) . selectedIndex == 5 )
document . querySelector ( ".box.refine form select[name='sort']" ) . disabled = true ;
else document . querySelector ( ".box.refine form select[name='sort']" ) . disabled = false ;
if ( document . querySelector ( ".box.refine form select[name='order']" ) . selectedIndex == 1 )
document . querySelector ( ".box.refine form select[name='order']" ) . disabled = true ;
else document . querySelector ( ".box.refine form select[name='order']" ) . disabled = false ;
if ( document . querySelector ( ".box.refine form select[name='sizeType']" ) . selectedIndex == 2 &&
document . querySelector ( ".box.refine form input[name='minSize']" ) . value == "" &&
document . querySelector ( ".box.refine form input[name='maxSize']" ) . value == "" )
document . querySelector ( ".box.refine form select[name='sizeType']" ) . disabled = true
else document . querySelector ( ".box.refine form select[name='sizeType']" ) . disabled = false
if ( document . querySelector ( ".box.refine form select[name='order']" ) . selectedIndex == 1 )
document . querySelector ( ".box.refine form select[name='order']" ) . disabled = true ;
else document . querySelector ( ".box.refine form select[name='order']" ) . disabled = false ;
} )
2017-07-21 01:47:36 +02:00
function humanFileSize ( bytes , si ) {
2017-08-26 04:15:28 +02:00
if ( bytes == 0 )
return "Unknown"
2017-07-21 01:47:36 +02:00
var k = si ? 1000 : 1024
var i = ~ ~ ( Math . log ( bytes ) / Math . log ( k ) )
2017-07-24 02:32:50 +02:00
return i == 0 ? bytes + " B" : ( bytes / Math . pow ( k , i ) ) . toFixed ( 1 ) + " " + "KMGTPEZY" [ i - 1 ] + ( si ? "" : "i" ) + "B"
2017-07-21 01:47:36 +02:00
}
2017-09-17 22:10:43 +02:00
function getCookieValue ( cookieName ) {
2017-10-14 03:16:34 +02:00
var startPos = document . cookie . indexOf ( cookieName + "=" )
if ( startPos == - 1 ) return ""
startPos += cookieName . length + 1
2017-09-17 22:10:43 +02:00
var endPos = document . cookie . substring ( startPos ) . indexOf ( ";" )
2017-10-14 03:16:34 +02:00
return endPos == - 1 ? document . cookie . substring ( startPos ) : document . cookie . substring ( startPos , endPos + startPos )
2017-09-17 22:10:43 +02:00
}
2017-06-04 07:43:41 +02:00
// @license-end