解决方法
1.关闭反垃圾保护(觉得没有什么用,而且用户体验不好)
2.修改/var/Widget/Security.php文件,在50行左右,修改为如下代码:
public function getTokenUrl($path)
{
$parts = parse_url($path);
$params = array();
if (!empty($parts['query'])) {
parse_str($parts['query'], $params);
}
$params['_'] = $this->getToken(preg_replace('/\?_pjax=#body/', '', $this->request->getRequestUrl()));
$parts['query'] = http_build_query($params);
return Typecho_Common::buildUrl($parts);
}CopyPHP
做到这一步就可以正常提交评论了
3.但是评论完会发现,网页地址显示xxx/comment
此时无法继续评论,应重新加载文章,才能继续评论
于是要在PJAX加载完之后执行以下代码:
var now_url_comment = location.href + ' ';
history.replaceState(null,'',now_url_comment.replace('/comment ',''));
这样就可以在评论提交后,将网页地址改回来
4.恢复 回复评论 功能
在主题的comments.php文件中,添加以下代码:
<script>respondId = '<?php $this->respondId(); ?>';</script>
然后在PJAX结束后执行以下代码:
window.TypechoComment = {
dom : function (id) {
return document.getElementById(id);
},
create : function (tag, attr) {
var el = document.createElement(tag);
for (var key in attr) {
el.setAttribute(key, attr[key]);
}
return el;
},
reply : function (cid, coid) {
var comment = this.dom(cid), parent = comment.parentNode,
response = this.dom(respondId()), input = this.dom('comment-parent'),
form = 'form' == response.tagName ? response : response.getElementsByTagName('form')[0],
textarea = response.getElementsByTagName('textarea')[0];
if (null == input) {
input = this.create('input', {
'type' : 'hidden',
'name' : 'parent',
'id' : 'comment-parent'
});
form.appendChild(input);
}
input.setAttribute('value', coid);
if (null == this.dom('comment-form-place-holder')) {
var holder = this.create('div', {
'id' : 'comment-form-place-holder'
});
response.parentNode.insertBefore(holder, response);
}
comment.appendChild(response);
this.dom('cancel-comment-reply-link').style.display = '';
if (null != textarea && 'text' == textarea.name) {
textarea.focus();
}
return false;
},
cancelReply : function () {
var response = this.dom(respondId()),
holder = this.dom('comment-form-place-holder'), input = this.dom('comment-parent');
if (null != input) {
input.parentNode.removeChild(input);
}
if (null == holder) {
return true;
}
this.dom('cancel-comment-reply-link').style.display = 'none';
holder.parentNode.insertBefore(response, holder);
return false;
}
};
此段代码从/var/Widget/Security.php文件中摘出
做了这些修改后,就可以恢复正常评论了。