Songsterr - Print-Enabler

Enable printing at songsterr.com for free accounts

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Songsterr - Print-Enabler
// @version      0.2.6
// @description  Enable printing at songsterr.com for free accounts
// @author       thedrunkendev
// @namespace    https://greasyfork.org/users/869634
// @grant        GM_addStyle
// @match        https://www.songsterr.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=https://songsterr.com/
// @match        https://songsterr.com/*
// ==/UserScript==

/*
This script has been forked from the following, with some tweaks / hacks added as it no longer works
https://greasyfork.org/de/scripts/369383-songsterr-print-enabler

An easy way to test the CSS in this script is to emulate the 'print' css media type. To do this,
open 'dev tools' -> 'more tools' -> rendering -> set 'Emulate CSS media type' to 'print'
*/

(function() {
    'use strict';

    for(var i=document.styleSheets[0].rules.length -1; i >0; i--){
        if(document.styleSheets[0].rules[i].cssText.indexOf("@media print") !=-1)
        {
            console.log("found @media print rule", i, document.styleSheets[0].href)
            console.log(document.styleSheets[0].rules[i].cssText)
        }
    }


    // Remove nag screens
    function removeNagScreens() {
        GM_addStyle('section section:not(#tablature) { display: none !important; }');
        GM_addStyle('header a[target="_blank"] { display: none !important; }');
        // This disables the "You need premium" message when printing
        GM_addStyle(`.Cdy160::after, .Cdy160::before{display: none !important;}`)
        GM_addStyle(`@media print { #promo{display: none !important; } }`)
    }

    // Show tabs
    function showTabsOnPrintView() {
        GM_addStyle('#tablature svg { display: block !important; }');
        // GM_addStyle('#tablature svg:not(:first-child) { display: block !important; }');
        GM_addStyle('@media print { #tablature svg g[data-label=cursor] { display: none !important; } }');

        // Set the height of the tabs so we can print them.
        // This can be a bit buggy but without this CSS, tabs won't get rendered at all
        // on the printed document.
        GM_addStyle(`@media print { .Cdy160 { height:unset; } }`);
    }

    // Enable print button
    function enablePrintButton() {
        try {
            GM_addStyle('.enabler-print > div[role=dialog] { display: none !important; }');

            var printElement = document.querySelector('#print-title-id').parentNode;
            printElement.parentNode.parentNode.classList.add('enabler-print');
            printElement.onclick = function(){window.print();};
        } catch(ex) {
            console.log("error enabling print button: " + ex);
        }
    }

    function enableAll() {
        removeNagScreens();
        showTabsOnPrintView();
        enablePrintButton();

        setTimeout(() => {
            var oldFn = document.querySelector("#control-print svg").onclick;
            document.querySelector("#control-print svg").onclick = () => {
                // HACKS: SVG is lazy loaded somehow. Scroll to the bottom to force it to render.
                //window.scrollTo({top: document.body.scrollHeight, behavior: 'smooth'});
                setTimeout(() => oldFn(), 1000)
            };
        },100)

    }

    function registrateOnLocationChange() {
        var pushState = history.pushState;
        history.pushState = function () {
            var changedUrl = arguments[2];
            pushState.apply(history, arguments);
            enableAll();
        };
    }
    registrateOnLocationChange();
    enableAll();
})();