Greasy Fork

Greasy Fork is available in English.

500px 图片下载

提供 500px 图片保存功能

当前为 2021-02-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         500px 图片下载
// @namespace    https://www.yffjglcms.com/
// @version      0.4.0.20210228
// @description  提供 500px 图片保存功能
// @author       yffjglcms
// @match        https://*.500px.com/photo/*
// @match        https://500px.com/photo/*
// @grant        none
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';

    // Your code h ere...

    // 图片选择器
    const imgSelector = ".photo-show__img"
    const corsApi = "https://cors.yffjglcms.workers.dev/?url="

    // 如果存在则执行
    function ifExist(selector, func){
        if($(selector)[0]==null)
        {
            setTimeout( function(){ifExist(selector, func);}, 5000);
        }
        else
        {
            func()
        }
    }

    // 添加导出按钮
    function addBtn() {
        let imgSrc = $(imgSelector).attr("src")

        // 下载按钮,导出按钮
        $(imgSelector).before(`<div style="display:inline;z-index:999;position: absolute;left: 10%;">
                <button class='Elements__OldButton-tze21g-0 czOfbc' style="display: block;" id="saveImg">下载图片</button>
                <button class='Elements__OldButton-tze21g-0 czOfbc' style="display: block;" id="openInNewTab">新窗打开</button>
        </div>`)


        //console.log(imgSrc)

        $("body").on("click", "#saveImg", ()=>{
            downloadImg(corsApi + encodeURIComponent(imgSrc) + '&filename='+ encodeURIComponent(document.title))
            
        })

        $("body").on("click", "#openInNewTab", ()=>{
            window.open(imgSrc)
        })
    }


    // 下载图片
    function downloadImg(src){
        var a=document.createElement('a');
        a.href=src
        a.download="file";
        a.click();
    }

    // 执行
    ifExist(imgSelector, addBtn)



})();