<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.randombell.com/index.php?action=history&amp;feed=atom&amp;title=User%3AJPxG%2FTrackSum.js</id>
	<title>User:JPxG/TrackSum.js - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.randombell.com/index.php?action=history&amp;feed=atom&amp;title=User%3AJPxG%2FTrackSum.js"/>
	<link rel="alternate" type="text/html" href="https://wiki.randombell.com/index.php?title=User:JPxG/TrackSum.js&amp;action=history"/>
	<updated>2026-04-27T15:24:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.randombell.com/index.php?title=User:JPxG/TrackSum.js&amp;diff=1554&amp;oldid=prev</id>
		<title>Randombell: Created page with &quot;// &lt;nowiki&gt; // TrackSum v1.0 // If you are editing a page, this script gives you a button to click and automatically sum up all the track lengths in Template:Tracklist to provide a total length. // Keep in mind that officially-released runtimes should be used if available. Some albums are longer or shorter than the sum of all track lengths. // NOTE: This won&#039;t work if you have hh:mm:ss track lengths. I don&#039;t know if this ever happens, but if it does, they need to be...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.randombell.com/index.php?title=User:JPxG/TrackSum.js&amp;diff=1554&amp;oldid=prev"/>
		<updated>2025-08-18T17:18:16Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;// &amp;lt;nowiki&amp;gt; // TrackSum v1.0 // If you are editing a page, this script gives you a button to click and automatically sum up all the track lengths in &lt;a href=&quot;/index.php?title=Template:Tracklist&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Template:Tracklist (page does not exist)&quot;&gt;Template:Tracklist&lt;/a&gt; to provide a total length. // Keep in mind that officially-released runtimes should be used if available. Some albums are longer or shorter than the sum of all track lengths. // NOTE: This won&amp;#039;t work if you have hh:mm:ss track lengths. I don&amp;#039;t know if this ever happens, but if it does, they need to be...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;// &amp;lt;nowiki&amp;gt;&lt;br /&gt;
// TrackSum v1.0&lt;br /&gt;
// If you are editing a page, this script gives you a button to click and automatically sum up all the track lengths in [[Template:Tracklist]] to provide a total length.&lt;br /&gt;
// Keep in mind that officially-released runtimes should be used if available. Some albums are longer or shorter than the sum of all track lengths.&lt;br /&gt;
// NOTE: This won&amp;#039;t work if you have hh:mm:ss track lengths. I don&amp;#039;t know if this ever happens, but if it does, they need to be in minutes.&lt;br /&gt;
// Of course, since 60 is 60, this will work on mm:ss as well as hh:mm track lengths, if they&amp;#039;re all used consistently.&lt;br /&gt;
// Skeleton of this code forked from Wikipedia:WikiProject User scripts/Scripts/Quick wikify&lt;br /&gt;
// - JPxG, 2021 September 3&lt;br /&gt;
&lt;br /&gt;
function getIndices(strang, substrang) {&lt;br /&gt;
    var indices = [], i = -1;&lt;br /&gt;
    while ((i = strang.indexOf(substrang, i+1)) != -1){&lt;br /&gt;
        indices.push(i);&lt;br /&gt;
    } // Close loop to increment over the strang.&lt;br /&gt;
    return indices;&lt;br /&gt;
} // Function to get indices of a substring in a string.&lt;br /&gt;
&lt;br /&gt;
function asMinSecs(input) {&lt;br /&gt;
	strangMins = Math.floor(input / 60);&lt;br /&gt;
	strangSecs = input - (strangMins * 60);&lt;br /&gt;
	if (strangSecs &amp;lt; 10) {&lt;br /&gt;
		return strangMins.toString() + &amp;quot;:0&amp;quot; + strangSecs.toString();&lt;br /&gt;
		}&lt;br /&gt;
	else { &lt;br /&gt;
		return strangMins.toString() + &amp;quot;:&amp;quot; + strangSecs.toString();&lt;br /&gt;
		}&lt;br /&gt;
} // Function to put out a string of minutes and seconds, given seconds as input.&lt;br /&gt;
&lt;br /&gt;
function doTracksum() {&lt;br /&gt;
	tbox = document.editform.wpTextbox1.value;&lt;br /&gt;
	// Get the text from the edit box and store it as &amp;quot;tbox&amp;quot;.&lt;br /&gt;
	obox = &amp;quot;&amp;quot;;&lt;br /&gt;
	// Initialize blank output string.&lt;br /&gt;
	var cursor = 0;&lt;br /&gt;
	var allTotal = 0;&lt;br /&gt;
	// Set cursor to zero.&lt;br /&gt;
	var indax = getIndices(tbox, &amp;quot;{{tracklist&amp;quot;);&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{Tracklist&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{Track listing&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{Track Listing&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{track listing&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{Tracklisting&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{tracklisting&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{Soundtrack&amp;quot;));&lt;br /&gt;
	indax = indax.concat(getIndices(tbox, &amp;quot;{{soundtrack&amp;quot;));&lt;br /&gt;
	indax = indax.sort();&lt;br /&gt;
	// We now have an array, &amp;quot;indax&amp;quot;, of every index where a tracklist template appears.&lt;br /&gt;
	obox = tbox.substring(0, indax[0]);&lt;br /&gt;
	// Store everything before the first tracklist template into obox.&lt;br /&gt;
	for (let i = 0; i &amp;lt; indax.length; i++){&lt;br /&gt;
		if (i == indax.length){&lt;br /&gt;
			j = indax.length;&lt;br /&gt;
		} else {&lt;br /&gt;
			j = indax[i+1];&lt;br /&gt;
		}&lt;br /&gt;
		slice = tbox.substring(indax[i], j);&lt;br /&gt;
		openbraces = getIndices(slice, &amp;quot;{{&amp;quot;);&lt;br /&gt;
		closebraces = getIndices(slice, &amp;quot;}}&amp;quot;);&lt;br /&gt;
		allbraces = openbraces.concat(closebraces).sort();&lt;br /&gt;
		lastbrace = allbraces[allbraces.length];&lt;br /&gt;
		// Get the last of all braces in the slice.&lt;br /&gt;
		braceStatus = 0;&lt;br /&gt;
		lastInSlice = 0;&lt;br /&gt;
		for(var incre = 0; incre &amp;lt; slice.length; incre++) {&lt;br /&gt;
			if(openbraces.includes(incre)) {braceStatus++}&lt;br /&gt;
			if(closebraces.includes(incre)) {braceStatus--}&lt;br /&gt;
			if(braceStatus == 0) {lastInSlice = incre;break}&lt;br /&gt;
		} // Try to find the closing brace of the whole darn slice. This is where we add the &amp;quot;total_length&amp;quot; param.&lt;br /&gt;
		total = 0;&lt;br /&gt;
		lengths = getIndices(slice, &amp;quot;|length&amp;quot;);&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;| length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|  length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|   length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|    length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|     length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|      length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|       length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|        length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|         length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|          length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|           length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|            length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|             length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|              length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|               length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|                length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|                 length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|                  length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|                   length&amp;quot;));&lt;br /&gt;
		lengths = lengths.concat(getIndices(slice, &amp;quot;|                    length&amp;quot;));&lt;br /&gt;
		//                                           12345678901234567890&lt;br /&gt;
		lengths = lengths.sort();&lt;br /&gt;
		console.log(lengths);&lt;br /&gt;
		// Now we have every index of &amp;quot;| length&amp;quot; in the template.&lt;br /&gt;
		for(var len = 0; len &amp;lt; lengths.length; len++) {&lt;br /&gt;
			time = 0;&lt;br /&gt;
			lenstr = slice.substring(lengths[len], slice.indexOf(&amp;quot;\n&amp;quot;, lengths[len]));&lt;br /&gt;
			// The string we&amp;#039;re looking at is between that instance of &amp;quot;length&amp;quot; and the next line return.&lt;br /&gt;
			lenstr = lenstr.substring(lenstr.indexOf(&amp;quot;=&amp;quot;)+1);&lt;br /&gt;
			// Everything in that string after the &amp;quot;=&amp;quot;.&lt;br /&gt;
			lenstr = lenstr.trim();&lt;br /&gt;
			// Remove spaces. Now it will just be &amp;quot;32:04&amp;quot; or &amp;quot;9:45&amp;quot; or whatever.&lt;br /&gt;
			mins = lenstr.substring(0, lenstr.indexOf(&amp;quot;:&amp;quot;));&lt;br /&gt;
			secs = lenstr.substring(lenstr.indexOf(&amp;quot;:&amp;quot;)+1);&lt;br /&gt;
			time = (parseInt(mins) * 60) + parseInt(secs);&lt;br /&gt;
			total = total + time;&lt;br /&gt;
		} // Read time out of the &amp;quot;length&amp;quot; parameter for every one we find.&lt;br /&gt;
		allTotal = allTotal + total;&lt;br /&gt;
		obox = obox + slice.substring(0,lastInSlice) + &amp;quot;| total_length    = &amp;quot; + asMinSecs(total) + &amp;quot; &amp;lt;!-- Automatically generated with User:JPxG/TrackSum.js --&amp;gt;\n&amp;quot; + slice.substring(lastInSlice);&lt;br /&gt;
		// Reconstitute the tbox.&lt;br /&gt;
	} // Everything in the darn obox.&lt;br /&gt;
	//obox = obox + tbox.substring(indax[indax.length])&lt;br /&gt;
	// Add the last bit of the tbox into obox.&lt;br /&gt;
	document.editform.wpTextbox1.value = obox;&lt;br /&gt;
	// document.editform.wpTextbox1.value = &amp;#039;{&amp;#039; + &amp;#039;{&amp;#039; + &amp;#039;Wikify|date=&amp;#039; + &amp;#039;{&amp;#039; + &amp;#039;{&amp;#039; + &amp;#039;subst:CURRENTMONTHNAME}} &amp;#039; + &amp;#039;{&amp;#039; + &amp;#039;{&amp;#039; + &amp;#039;subst:CURRENTYEAR}}}}\n\n&amp;#039; + document.editform.wpTextbox1.value;&lt;br /&gt;
	document.editform.wpSummary.value = &amp;quot;Automatically summed track lengths (total length: &amp;quot; + asMinSecs(allTotal) + &amp;quot;), using script [JPxG&amp;#039;s [[User:JPxG/TrackSum.js|TrackSum]] V1.0]&amp;quot;;&lt;br /&gt;
	document.editform.wpMinoredit.checked = true;&lt;br /&gt;
	//document.editform.submit();&lt;br /&gt;
} // function to replace the stuff with the other stuff&lt;br /&gt;
&lt;br /&gt;
addOnloadHook(function() {&lt;br /&gt;
	if (document.editform) {&lt;br /&gt;
		mw.util.addPortletLink(&amp;quot;p-cactions&amp;quot;, &amp;quot;javascript:doTracksum()&amp;quot;, &amp;quot;Tracksum&amp;quot;, &amp;quot;ca-tracksum&amp;quot;, &amp;quot;Sum up track lengths in tracklist template&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}); // onloadhook&lt;br /&gt;
// &amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Randombell</name></author>
	</entry>
</feed>