MaGuitarDownloadImage

下載嘖嘖的影片和圖片

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MaGuitarDownloadImage
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  下載嘖嘖的影片和圖片
// @author       You
// @match        https://www.zeczec.com/projects/mashushu-rock/updates/*
// @grant        none
// @require http://code.jquery.com/jquery-1.9.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/downloadjs/1.4.8/download.js
// @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// ==/UserScript==

(function() {
        waitForKeyElements('h3:contains("熱門單曲")', CopyTitleContent);
        waitForKeyElements('.nested-media img[src*="jpg"]', WaitForImage);
})();

function CopyTitleContent(){
    var title = $('h3:contains("熱門單曲")');
        title.append("<input id=\"copyTitle\" type=\"button\" value=\"複製標題\">");
    $("#copyTitle").click(function(){
            copyTitle();
        });
}

function WaitForImage(){
        if($("#downloadImgs").length == 0){
            $("label:contains('搜尋')").parent().prepend("<input id=\"downloadImgs\" type=\"button\" value=\"下載教學圖片\">");
            $("#downloadImgs").click(function(){
                 downloadAll();
             });
        }

}

function GetImages(){
    var result = [];
    $('.nested-media img[src*="jpg"]').each(function(index, element) {
         if($(element).attr("src").indexOf("jpg") != -1){
              result.push($(element).attr("src"));
         }
    });
    return result;
}

function copyTitle(){
    var title = getTitle()
    copyToClipboard(title);

}

String.prototype.filename=function(extension){
    var s= this.replace(/\\/g, '/');
    s= s.substring(s.lastIndexOf('/')+ 1);
    var filename = getTitle()+' '+ (extension? s.replace(/[?#].+$/, ''): s.split('.')[0]);
    return filename;
}

function downloadAll() {
     var images = GetImages();
    $(images).each(function(index, element) {
        forceDownload(element,element.filename());
    });
}

function forceDownload(url, fileName){
    var downloadUrl = 'https://cors-anywhere.herokuapp.com/' + url;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", downloadUrl, true);
    xhr.responseType = "blob";
    xhr.onload = function(){
        var urlCreator = window.URL || window.webkitURL;
        var imageUrl = urlCreator.createObjectURL(this.response);
        var tag = document.createElement('a');
        tag.href = imageUrl;
        tag.download = fileName;
        document.body.appendChild(tag);
        tag.click();
        document.body.removeChild(tag);
    }
    xhr.send();
}


function getTitle(){
    var title = $('h3:contains("熱門單曲")').text();
    var myRegexp = /【熱門單曲】(.*)\(/g;
    var match = myRegexp.exec(title);
    return match[1];
}

function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        // IE specific code path to prevent textarea being shown while dialog is visible.
        return clipboardData.setData("Text", text);

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
}