258 lines
8.4 KiB
JavaScript
258 lines
8.4 KiB
JavaScript
|
jQuery(document).ready(function($) {
|
|||
|
|
|||
|
/**
|
|||
|
* 初始化字符计数器
|
|||
|
*/
|
|||
|
function initCharCounters() {
|
|||
|
// 标题计数器
|
|||
|
$('#ai_seo_title').on('input', function() {
|
|||
|
updateCounter(this, '#title-counter', 60);
|
|||
|
updatePreview();
|
|||
|
}).trigger('input');
|
|||
|
|
|||
|
// 描述计数器
|
|||
|
$('#ai_seo_description').on('input', function() {
|
|||
|
updateCounter(this, '#desc-counter', 160);
|
|||
|
updatePreview();
|
|||
|
}).trigger('input');
|
|||
|
|
|||
|
// 关键词计数器
|
|||
|
$('#ai_seo_keywords').on('input', function() {
|
|||
|
updateCounter(this, '#keywords-counter', 100);
|
|||
|
}).trigger('input');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 更新字符计数器
|
|||
|
*/
|
|||
|
function updateCounter(input, counterSelector, maxLength) {
|
|||
|
var length = $(input).val().length;
|
|||
|
var counter = $(counterSelector);
|
|||
|
var percentage = (length / maxLength) * 100;
|
|||
|
|
|||
|
counter.text(length + '/' + maxLength);
|
|||
|
|
|||
|
// 根据长度设置颜色
|
|||
|
counter.removeClass('warning good');
|
|||
|
if (percentage > 90) {
|
|||
|
counter.addClass('warning');
|
|||
|
} else if (percentage >= 70) {
|
|||
|
counter.addClass('good');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 更新搜索结果预览
|
|||
|
*/
|
|||
|
function updatePreview() {
|
|||
|
var title = $('#ai_seo_title').val() || $('#title').val() || '未设置标题';
|
|||
|
var description = $('#ai_seo_description').val() || '暂无描述';
|
|||
|
|
|||
|
$('#preview-title').text(title);
|
|||
|
$('#preview-description').text(description);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 生成SEO内容
|
|||
|
*/
|
|||
|
$('#generate-seo-btn').on('click', function() {
|
|||
|
var button = $(this);
|
|||
|
var postId = $('#post_ID').val();
|
|||
|
|
|||
|
if (!postId) {
|
|||
|
alert('请先保存文章草稿后再生成SEO内容');
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// 显示加载状态
|
|||
|
button.prop('disabled', true);
|
|||
|
$('#ai-seo-loading').show();
|
|||
|
$('.ai-seo-fields').css('opacity', '0.5');
|
|||
|
|
|||
|
// 发送AJAX请求
|
|||
|
$.ajax({
|
|||
|
url: aiSeoAjax.ajax_url,
|
|||
|
type: 'POST',
|
|||
|
timeout: 120000, // 设置超时时间为120秒(2分钟)
|
|||
|
data: {
|
|||
|
action: 'generate_seo_content',
|
|||
|
post_id: postId,
|
|||
|
nonce: aiSeoAjax.nonce
|
|||
|
},
|
|||
|
success: function(response) {
|
|||
|
if (response.success && response.data) {
|
|||
|
// 填充生成的内容
|
|||
|
if (response.data.title) {
|
|||
|
$('#ai_seo_title').val(response.data.title).trigger('input');
|
|||
|
}
|
|||
|
if (response.data.description) {
|
|||
|
$('#ai_seo_description').val(response.data.description).trigger('input');
|
|||
|
}
|
|||
|
if (response.data.keywords) {
|
|||
|
$('#ai_seo_keywords').val(response.data.keywords).trigger('input');
|
|||
|
}
|
|||
|
|
|||
|
// 显示成功消息
|
|||
|
showNotice('SEO内容生成成功!', 'success');
|
|||
|
} else {
|
|||
|
showNotice('生成失败:' + (response.data || '未知错误'), 'error');
|
|||
|
}
|
|||
|
},
|
|||
|
error: function(xhr, status, error) {
|
|||
|
var errorMessage = '请求失败:';
|
|||
|
if (status === 'timeout') {
|
|||
|
errorMessage = 'AI生成超时,请稍后重试。如果问题持续存在,请联系管理员。';
|
|||
|
} else if (status === 'error') {
|
|||
|
errorMessage = '网络错误,请检查网络连接后重试。';
|
|||
|
} else {
|
|||
|
errorMessage += error;
|
|||
|
}
|
|||
|
showNotice(errorMessage, 'error');
|
|||
|
},
|
|||
|
complete: function() {
|
|||
|
// 恢复界面状态
|
|||
|
button.prop('disabled', false);
|
|||
|
$('#ai-seo-loading').hide();
|
|||
|
$('.ai-seo-fields').css('opacity', '1');
|
|||
|
}
|
|||
|
});
|
|||
|
});
|
|||
|
|
|||
|
/**
|
|||
|
* 显示通知消息
|
|||
|
*/
|
|||
|
function showNotice(message, type) {
|
|||
|
var noticeClass = type === 'success' ? 'notice-success' : 'notice-error';
|
|||
|
var notice = $('<div class="notice ' + noticeClass + ' is-dismissible"><p>' + message + '</p></div>');
|
|||
|
|
|||
|
$('.ai-seo-meta-box').prepend(notice);
|
|||
|
|
|||
|
// 自动移除通知
|
|||
|
setTimeout(function() {
|
|||
|
notice.fadeOut(function() {
|
|||
|
notice.remove();
|
|||
|
});
|
|||
|
}, 5000);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 复制到剪贴板功能
|
|||
|
*/
|
|||
|
function addCopyButtons() {
|
|||
|
$('.ai-seo-field').each(function() {
|
|||
|
var field = $(this);
|
|||
|
var input = field.find('input, textarea');
|
|||
|
|
|||
|
if (input.length) {
|
|||
|
var copyBtn = $('<button type="button" class="button button-small copy-btn" title="复制到剪贴板">📋</button>');
|
|||
|
field.find('label').append(copyBtn);
|
|||
|
|
|||
|
copyBtn.on('click', function(e) {
|
|||
|
e.preventDefault();
|
|||
|
input.select();
|
|||
|
document.execCommand('copy');
|
|||
|
|
|||
|
var originalText = copyBtn.text();
|
|||
|
copyBtn.text('✅');
|
|||
|
setTimeout(function() {
|
|||
|
copyBtn.text(originalText);
|
|||
|
}, 1000);
|
|||
|
});
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 键盘快捷键
|
|||
|
*/
|
|||
|
$(document).on('keydown', function(e) {
|
|||
|
// Ctrl+Shift+G 生成SEO内容
|
|||
|
if (e.ctrlKey && e.shiftKey && e.keyCode === 71) {
|
|||
|
e.preventDefault();
|
|||
|
$('#generate-seo-btn').click();
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
/**
|
|||
|
* 自动保存功能
|
|||
|
*/
|
|||
|
var autoSaveTimeout;
|
|||
|
$('.ai-seo-field input, .ai-seo-field textarea').on('input', function() {
|
|||
|
clearTimeout(autoSaveTimeout);
|
|||
|
autoSaveTimeout = setTimeout(function() {
|
|||
|
// 这里可以添加自动保存逻辑
|
|||
|
console.log('Auto-saving SEO data...');
|
|||
|
}, 2000);
|
|||
|
});
|
|||
|
|
|||
|
/**
|
|||
|
* 验证SEO内容质量
|
|||
|
*/
|
|||
|
function validateSeoContent() {
|
|||
|
var title = $('#ai_seo_title').val();
|
|||
|
var description = $('#ai_seo_description').val();
|
|||
|
var keywords = $('#ai_seo_keywords').val();
|
|||
|
|
|||
|
var issues = [];
|
|||
|
|
|||
|
if (!title || title.length < 30) {
|
|||
|
issues.push('标题太短,建议至少30个字符');
|
|||
|
}
|
|||
|
if (title && title.length > 60) {
|
|||
|
issues.push('标题太长,建议不超过60个字符');
|
|||
|
}
|
|||
|
if (!description || description.length < 120) {
|
|||
|
issues.push('描述太短,建议至少120个字符');
|
|||
|
}
|
|||
|
if (description && description.length > 160) {
|
|||
|
issues.push('描述太长,建议不超过160个字符');
|
|||
|
}
|
|||
|
if (!keywords) {
|
|||
|
issues.push('建议添加相关关键词');
|
|||
|
}
|
|||
|
|
|||
|
return issues;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 显示SEO质量评分
|
|||
|
*/
|
|||
|
function showSeoScore() {
|
|||
|
var issues = validateSeoContent();
|
|||
|
var score = Math.max(0, 100 - (issues.length * 20));
|
|||
|
|
|||
|
var scoreHtml = '<div class="seo-score">';
|
|||
|
scoreHtml += '<h4>SEO质量评分: <span class="score-' + (score >= 80 ? 'good' : score >= 60 ? 'ok' : 'poor') + '">' + score + '/100</span></h4>';
|
|||
|
|
|||
|
if (issues.length > 0) {
|
|||
|
scoreHtml += '<ul class="seo-issues">';
|
|||
|
issues.forEach(function(issue) {
|
|||
|
scoreHtml += '<li>⚠️ ' + issue + '</li>';
|
|||
|
});
|
|||
|
scoreHtml += '</ul>';
|
|||
|
} else {
|
|||
|
scoreHtml += '<p class="seo-perfect">✅ SEO内容质量良好!</p>';
|
|||
|
}
|
|||
|
|
|||
|
scoreHtml += '</div>';
|
|||
|
|
|||
|
$('.seo-score').remove();
|
|||
|
$('.ai-seo-tips').before(scoreHtml);
|
|||
|
}
|
|||
|
|
|||
|
// 初始化所有功能
|
|||
|
initCharCounters();
|
|||
|
updatePreview();
|
|||
|
addCopyButtons();
|
|||
|
|
|||
|
// 监听内容变化以更新评分
|
|||
|
$('.ai-seo-field input, .ai-seo-field textarea').on('input', function() {
|
|||
|
setTimeout(showSeoScore, 100);
|
|||
|
});
|
|||
|
|
|||
|
// 初始评分
|
|||
|
showSeoScore();
|
|||
|
|
|||
|
// 添加提示信息
|
|||
|
$('.ai-seo-header').append('<small style="color: #666; margin-left: 10px;">快捷键: Ctrl+Shift+G</small>');
|
|||
|
});
|