[前端] 配置博客园搜索功能实现新标签页打开搜索结果

博客园有一个找找看功能,作用是搜索博主自己的文章。但是,它的搜索结果是在当前页打开,有时候需要新标签页打开,这个可以通过自定义js代码来实现。

打开找找看

这个选项位于后台 -> 选项 -> 侧边栏控件 勾上找找看,博客首页就会出现一个搜索框

自定义js代码

将这部分代码追加至后台 -> 设置 -> 页脚HTML代码中即可

<script>
    function open_url(new_tab) {
        let url = "https://zzk.cnblogs.com/s?w="+encodeURIComponent("blog:" + currentBlogApp + " " + document.getElementById("q").value);
        const a = document.createElement('a');
        a.href = url;
        if (new_tab)
            a.target = '_blank';           // 新标签页
        a.rel = 'noopener noreferrer'; // 安全
        document.body.appendChild(a);  // 必须先插到 DOM
        a.click();                     // 立即触发
        document.body.removeChild(a);  // 用完即丢
    }
    zzk_go = function () {
        open_url(false); // 为true时新标签页打开
    }

    zzk_go_enter = function (event) {
        console.log(event);
        if (event.key == "Enter")
            open_url(true);
    }
</script>

其原理是重写了点击按钮的方法zzk_go和回车的方法 zzk_go_enter

如上代码当前实现的功能是:点击按钮当前页打开结果,按下回车新标签打开结果,如果按着 shift按下回车则后台新标签页打开。

不使用window.open函数是因为博客园似乎限制了这类函数,当出现在自定义代码中时整个自定义代码不生效。

具体效果可在我的博客首页试用。

posted @ 2025-09-02 16:29  Startu  阅读(33)  评论(0)    收藏  举报