UG tab download

download plain text tab from ultimate-guitar.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name UG tab download
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  download plain text tab from ultimate-guitar.com
// @author       mihau
// @include https://tabs.ultimate-guitar.com/tab/*
// @license MIT
// ==/UserScript==

// from https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
function waitforit(selector) {
  return new Promise(resolve => {
    if (document.querySelector(selector)) {
      return resolve(document.querySelector(selector));
    }

    var observer = new MutationObserver(mutations => {
      if (document.querySelector(selector)) {
        observer.disconnect();
        resolve(document.querySelector(selector));
      }
    });

    // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
    observer.observe(document.body, { childList: true, subtree: true });
  });
}

waitforit('#pdfControl').then((elm) => {
  
  var tab = window.UGAPP.store.page["data"]["tab_view"]["wiki_tab"]["content"].replace(/\[ch\]/gi, "").replace(/\[\/ch\]/gi, "");
 
  var hiddenElement = document.createElement('a');
  var format = encodeURIComponent(tab.replace(/\[tab\]/g, "").replace(/\[.tab\]/g, ""));
  hiddenElement.href = 'data:attachment/text,' + format;
  hiddenElement.download = location.href.replace(/.*tab\//,"").replace(/\-/g,"_").replace(/\//g,"-").replace(/_tabs/g,"\.tab").replace(/tab_.*/,"tab");

  document.querySelectorAll(".M71za.zQu4a.IlvQM.kvB02.yjpiY")[0].innerHTML = '<button type="button" tabindex="0" id="tabdl" class="H2hsN vDzLP jcUeD Kb0iM"><svg aria-hidden="true" viewBox="0 0 20 20" class="qkTwy"><g><path d="M4 16h12v-3h2v5H2v-5h2z"></path><path d="M11 8h3l-4 6-4-6h3V3h2z"></path></g></svg><span class="Q_TBK RIR92 cH1R2 cTzGe VLnCY p62dr">Save .tab</span></button>';

  document.getElementById("tabdl").onclick = function() {
    hiddenElement.click();
  }

});