iCity 网页版小助手

优化网页版 iCity 日记体验

目前为 2020-09-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         iCity 网页版小助手
// @namespace    https://limxw.com
// @version      0.3
// @description  优化网页版 iCity 日记体验
// @author       WingLim
// @match        https://icity.ly/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var $ = window.jQuery

    // 自动增高 textarea 高度
    const textarea = $(".content")
    // 隐藏滑动条
    textarea.css("overflow", "hidden")

    // 计算内容行数
    function calcHeight(value) {
        let numberOfLineBreaks = (value.match(/\n/g) || []).length;
        let lines = 0
        if (numberOfLineBreaks > 4) {
            lines = numberOfLineBreaks - 4
        }
        console.log("lines", lines)
        console.log("breaks", numberOfLineBreaks)
        // min-height + lines x line-height
        let newHeight = 126 + lines * 24;
        return newHeight;
    }
    textarea.on("keyup", function() {
        textarea.css("transition", "height 200ms")
        textarea.height(calcHeight(textarea.val()))
    })

    textarea.on("focusout", function() {
        textarea.css("transition", "")
        textarea.height(96)
    })

    // 拖放图片
    const postComposer = $(".post-composer");

    postComposer.on("dragenter", function(e) {
            e.preventDefault();
        }
    );

    postComposer.on("dragover", function(e) {
            e.preventDefault();
        }
    );

    postComposer.on("drop", function(e) {
            dropHandler(e);
        }
    );

    var dropHandler = function(e) {
        e.preventDefault(); // 获取文件列表
        var fileList = e.originalEvent.dataTransfer.files;

        // 检测是否是拖拽文件到页面的操作
        if (fileList.length == 0) {
            return;
        }

        // 检测文件是不是图片
        if (fileList[0].type.indexOf("image") === -1) {
            return;
        }

        // 实例化file reader对象
        let reader = new FileReader();
        const photosQueue = $(".photos-queue")
        postComposer.addClass("with-photos")

        // 预览图片
        reader.onload = function(e) {
            var div = $('<div class="photo-one"></div>'),
                removeBtn = $('<a class="remove" href="#"><i class="fa fa-remove"></i></a>'),
                img = $("<img>")
                img[0].src = this.result
                img.data("file", fileList[0])
                div.append(img)
                div.append(removeBtn)
                photosQueue.append(div)
        };
        reader.readAsDataURL(fileList[0]);
    };

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址