登录 立即注册

首页 > 绿虎论坛 > 网页插件 (发帖)

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

作者: @Ta

时间: 2021-04-14发布,2022-06-17修改

点击: 12836

装好后 点我测试 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|隐藏机器人聊天)』

1.

@老虎会游泳 反馈bug:
代码片段中的 @xxx 被翻译

<a class="userat" href="#" onclick="atAdd('老虎会游泳',this);return false">@</a><a class="userinfo" href="user.info.1.html">老虎会游泳</a> 

小米MIX2s(白)

(/@Ta/2021-04-14 12:07//)

2.

@水木易安,哪里?

(/@Ta/2021-04-14 12:19//)

3.

@老虎会游泳

出问题后 我删掉了.

我说的代码片段是指"~ ~" 这种

image.png

小米MIX2s(白)

(/@Ta/2021-04-14 12:23//)

4.

@水木易安,虎绿林UBB不支持使用波浪线开始代码片段,必须使用反引号。使用波浪线开始的代码片段不会被保护,所以会出现你现在看到的这种效果。使用8个空格开始的代码片段也一样。

```
@老虎会游泳
```
@老虎会游泳
(/@Ta/2021-04-14 12:24//)

5.

@老虎会游泳,好吧~

小米MIX2s(白)

(/@Ta/2021-04-14 12:23//)

6.


如果是直接外链所在页面弹模态框提示 然后点击新标签跳转,这样实现有没啥弊端

(/@Ta/2021-04-14 13:44//)

7.

@000,不会写。
小米MIX2s(白)

(/@Ta/2021-04-15 09:41//)

9. 请问咋安装哦?没看懂啊
(/@Ta/2021-12-30 21:38//)

回复需要登录

5月15日 20:03 星期三

本站由hu60wap6华为CPU驱动

备案号: 京ICP备18041936号-1