目前应用的 Handsome 主题,简洁优雅,美观大方,不足之处是主题自带的搜索组件,搜文章有 Bug,如果一篇文章被编辑过多次,则结果列表会出现多个条目,需要通过重建索引来修复。但默认的操作路径:

管理 -> 控制台 -> 插件 -> 设置 -> 滚动页面 -> 构建文章索引

需要六步,不胜繁琐,能否简化呢?

我通过浏览器控制台看了下插件的源码:

<button type="submit" class="btn btn-s btn-warn btn-operate" formaction="https://hiclick.cn/admin/options-plugin.php?config=Handsome&action=buildSearchIndex">构建文章索引</button>

发现构建索引并不依赖其它数据,似乎只是请求一个接口而已,我尝试直接在浏览器执行 Form Action URL,系统提示“写入搜索数据成功”,看来有捷径。

于是我在管理主界面的系统设置之后,添加了一行代码:

<li><a href="https://hiclick.cn/admin/options-plugin.php?config=Handsome&action=buildSearchIndex"><?php _e('构建索引'); ?></a></li>

重建索引正常执行,但又发现新的问题,控制台报 Typecho 检查更新出错,难道我修改代码的时候,又动了 JavaScript?恢复文件,问题依旧,但这次有了教训,不再纠结,很显然这个问题一早就存在了,只是我没有发现而已。

检查更新出错,可能是 Typecho 官方接口出了事,而我现在并无计划升级主程序(甚至连追跟主题的心思都没了),那就干脆禁用升级检查。

$(document).ready(function () {
    var ul = $('#typecho-message ul'), cache = window.sessionStorage,
        html = cache ? cache.getItem('feed') : '',
        update = cache ? cache.getItem('update') : '';

    if (!!html) {
        ul.html(html);
    } else {
        html = '';
        $.get('<?php $options->index('/action/ajax?do=feed'); ?>', function (o) {
            for (var i = 0; i < o.length; i ++) {
                var item = o[i];
                html += '<li><span>' + item.date + '</span> <a href="' + item.link + '" target="_blank">' + item.title
                    + '</a></li>';
            }

            ul.html(html);
            cache.setItem('feed', html);
        }, 'json');
    }

    function applyUpdate(update) {
        if (update.available) {
            $('<div class="update-check message error"><p>'
                + '<?php _e('您当前使用的版本是 %s'); ?>'.replace('%s', update.current) + '<br />'
                + '<strong><a href="' + update.link + '" target="_blank">'
                + '<?php _e('官方最新版本是 %s'); ?>'.replace('%s', update.latest) + '</a></strong></p></div>')
            .insertAfter('.typecho-page-title').effect('highlight');
        }
    }

    if (!!update) {
        //applyUpdate($.parseJSON(update));
    } else {
        $.get('<?php $options->index('/action/ajax?do=checkVersion'); ?>', function (o, status, resp) {
            //applyUpdate(o);
            cache.setItem('update', resp.responseText);
        }, 'json');
    }
});

注释 applyUpdate 即可。

另外,第一次注意到 button 的 formaction 属性,菜鸟教程给了一个例子,带有两个提交按钮的表单(带有不同的 action),第一个提交按钮将表单数据提交到 "demo_form.html", 第二个提交按钮将表单数据提交到 "demo_admin.html":

<form action="demo_form.html" method="get">
  First name: <input type="text" name="fname" /><br />
  Last name: <input type="text" name="lname" /><br />
  <button type="submit">提交</button><br />
  <button type="submit" formaction="demo_admin.html">提交</button>
</form>

问题让我进步。

最后修改:2023 年 01 月 31 日
如果觉得我的文章对你有用,请随意赞赏