Soundgasm Progress Bar

Adds a progress bar when uploading a file to Soundgasm. Also displays an estimated time of completion.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Soundgasm Progress Bar
// @description  Adds a progress bar when uploading a file to Soundgasm. Also displays an estimated time of completion.
// @namespace    RoundPension
// @include      https://soundgasm.net/upload
// @version      1.0
// @license      MIT-0
// ==/UserScript==

var form = document.querySelector("form");

var req = new XMLHttpRequest();
var progbar;
var eta;
var starttime;
var lasttime;

form.addEventListener("submit", function(e) {
  e.preventDefault();
  
  req.upload.addEventListener("progress", prog);
  req.upload.addEventListener("loadstart", start);
  req.addEventListener("load", end);
  req.open("POST", "https://soundgasm.net/upload");
  req.send(new FormData(form));
});

function start() {
  starttime = Date.now();
  lasttime = starttime;
  var div = document.createElement("div");
  document.querySelector("body").append(div);
  div.innerHTML += `
<label>
  Uploading File: <progress id="prog" value="0"></progress>
</label>
<p>Estimated time remaining: <span id="eta"></span></p>`;
  progBar = document.getElementById("prog");
  eta = document.getElementById("eta");
}

function prog(e) {
  var ratio = e.loaded/e.total
  progBar.value = ratio;
  if (Date.now() - lasttime > 1000) {
    updateETA(ratio);
  }
}

function end() {
  document.querySelector("body").innerHTML = `
<p>Upload completed!</p>
<p><a href="https://soundgasm.net/upload">Upload another</a></p>`
}

function updateETA(ratio) {
  lasttime = Date.now();
  var totalTime = (Date.now() - starttime)/ratio;
  var etaTime = totalTime*(1-ratio);
  eta.innerHTML = msToTime(etaTime);
}

//modified from https://stackoverflow.com/a/19700358
function msToTime(duration) {
  var seconds = Math.floor((duration / 1000) % 60),
    minutes = Math.floor((duration / (1000 * 60)) % 60),
    hours = Math.floor(duration / (1000 * 60 * 60));
  
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds;
}