[网页插件] 新窗口打开链接

@Ta 2021-04-14发布,2022-06-17修改 16433点击

装好后 点我测试 http://www.baidu.com

image.png


导入网页插件:新窗口打开链接(当前用户:2,总安装次数:3)
<script>
class NewTarget {
    cookieName = 'new_target';
    isOpen = Boolean(this.getCookie(this.cookieName));

    init() {
        if (location.pathname.includes('link.url.')) {
            const a = document.querySelector('.content a');
            const {href} = a;
            a.href = "javascript:void(0);";
            a.onclick = () => {
                this.open(href);
            };

            const checkBox = document.createElement('input');
            checkBox.type = 'checkbox';

            if (this.isOpen) {
                checkBox.checked = true;
            } else {
                delete checkBox.checked;
            }

            checkBox.onchange = ({target: {checked}}) => {
                this.setCookie(this.cookieName, checked);
                this.isOpen = checked;
            }
            const div = document.createElement('div');
            div.innerText = '是否新窗口打开:'
            div.appendChild(checkBox)


            a.parentNode.appendChild(div)
        }
    }

    open(href) {
        window.open(href, this.isOpen ? "_blank" : "_self");
        if (this.isOpen) {
            history.go(-1);
        }
    }

    /**
     * 设置cookie
     */
    setCookie(name, value) {
        const Days = 30;
        const exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    /**
     * 读取cookie
     */
    getCookie(name) {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) return unescape(arr[2]);
        else return null;
    }
}

document.addEventListener('DOMContentLoaded', () => {
    var nt = new NewTarget();
    nt.init();
})
</script>

小米MIX2s(白)

回复列表(8|隐藏机器人聊天)
添加新回复
回复需要登录