juejinBackgroundImage

修改掘金背景图,设置自己喜欢的背景图!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         juejinBackgroundImage
// @namespace    http://tampermonkey.net/
// @version      0.2.1
// @description  修改掘金背景图,设置自己喜欢的背景图!
// @author       wjh
// @match        https://juejin.cn
// @match        https://juejin.cn/*
// @icon         https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/849e3902e63a42cea93abb448cb0bb0f~tplv-k3u1fbpfcp-watermark.image
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.2.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    $(document).ready(function(){
        // 图片路径
        let imgUrl = window.localStorage.getItem('imgUrl') || 'https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9d58e69aaefa4aa59fdd61f1a58ede84~tplv-k3u1fbpfcp-watermark.image'
        let uploadBox = "<div id='uploadBox' style='position:fixed;top:80px;right:20px;background:rgba(255,255,255,0.5);padding: 10px;'><input style='width:66px;' type='file' name='' id='files'></div>"

        $('#juejin').css('backgroundImage',`url(${imgUrl})`);
        $('#juejin').css('backgroundSize',`100%`);

        $('body').append(uploadBox);


        $("#files").change(function(e) {
            readFileAsDataURL($('#files')[0].files[0]).then(dataURL => {
                window.localStorage.setItem('imgUrl',dataURL)
                $('#juejin').css('backgroundImage',`url(${dataURL})`);
            });
        })
    })


    function readFileAsDataURL(file) {
        return new Promise((resolve, reject) => {
            let reader = new FileReader();

            reader.onload = function(event) {
                resolve(event.target.result);
            };

            reader.onerror = function(event) {
                reject(new Error("File could not be read! Code " + event.target.error.code));
            };

            reader.readAsDataURL(file);
        });
    }



    // Your code here...
})();