Difference between revisions of "MediaWiki:Common.js"
From UKTAPS.CO.UK Wiki
(Created page with "→Any JavaScript here will be loaded for all users on every page load.: /** * SEO 自动 Canonical 标签配置 * 作用:将特定指南页的权重回流至主站产...") |
|||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
| − | |||
| − | |||
| − | |||
| − | |||
$(document).ready(function() { | $(document).ready(function() { | ||
| − | + | var rawUrl = $('.canonical-link').data('href'); | |
| − | var | + | |
| − | + | if (rawUrl) { | |
| − | // | + | // 自动清洗:去掉可能误写的方括号和 Markdown 语法 |
| − | + | var cleanUrl = rawUrl.toString().replace(/[\[\]]/g, ''); | |
| − | + | ||
| + | // 如果是 Markdown 链接格式,再次清洗 | ||
| + | if (cleanUrl.indexOf('](') > -1) { | ||
| + | cleanUrl = cleanUrl.split('](')[1].replace(')', ''); | ||
| + | } | ||
| − | + | $('link[rel="canonical"]').remove(); | |
| − | + | ||
var link = document.createElement('link'); | var link = document.createElement('link'); | ||
link.setAttribute('rel', 'canonical'); | link.setAttribute('rel', 'canonical'); | ||
| − | link.setAttribute('href', | + | link.setAttribute('href', cleanUrl); |
document.head.appendChild(link); | document.head.appendChild(link); | ||
| − | console.log('SEO: Canonical | + | |
| + | console.log('SEO: Canonical successfully cleaned and linked to: ' + cleanUrl); | ||
} | } | ||
}); | }); | ||
Latest revision as of 13:54, 15 June 2026
/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function() {
var rawUrl = $('.canonical-link').data('href');
if (rawUrl) {
// 自动清洗:去掉可能误写的方括号和 Markdown 语法
var cleanUrl = rawUrl.toString().replace(/[\[\]]/g, '');
// 如果是 Markdown 链接格式,再次清洗
if (cleanUrl.indexOf('](') > -1) {
cleanUrl = cleanUrl.split('](')[1].replace(')', '');
}
$('link[rel="canonical"]').remove();
var link = document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', cleanUrl);
document.head.appendChild(link);
console.log('SEO: Canonical successfully cleaned and linked to: ' + cleanUrl);
}
});