MediaWiki:Common.js:修订间差异
来自生物多样性知识平台
无编辑摘要 |
无编辑摘要 |
||
(未显示另一用户的1个中间版本) | |||
第19行: | 第19行: | ||
} | } | ||
// 加载 Keycloak JS 库并手动设置跨域 Cookie | |||
$.getScript('https://cdnjs.cloudflare.com/ajax/libs/keycloak-js/16.1.1/keycloak.min.js', function() { | |||
// 初始化 Keycloak | |||
const keycloak = new Keycloak({ | |||
url: 'https://sso.biodiversity.cern.ac.cn/auth', | |||
realm: 'biodiversity', | |||
clientId: 'wiki-client' | |||
}); | |||
}); | |||
keycloak.init({ | |||
onLoad: 'check-sso', | |||
promiseType: 'native' // Optional: Use native Promises for better compatibility | |||
}).then(function(authenticated) { | |||
if (authenticated) { | |||
console.log('Authenticated'); | |||
checkKeycloakLogin(); | |||
performMediaWikiLogin(); | |||
} else { | |||
console.log('Not authenticated'); | |||
} | |||
}).catch(function() { | |||
console.log('Failed to initialize Keycloak'); | |||
}); | |||
// Function to trigger MediaWiki login | |||
function performMediaWikiLogin() { | |||
const currentUrl = window.location.href; | |||
const loginUrl = 'https://wiki.biodiversity.cern.ac.cn/index.php/Special:PluggableAuthLogin'; | |||
if (!currentUrl.includes('Special:PluggableAuthLogin')) { | |||
window.location.href = loginUrl; | |||
} | |||
} | |||
// 检查 Keycloak 登录状态并设置 Cookie | |||
function checkKeycloakLogin() { | |||
var keycloakSession = getCookie("KEYCLOAK_SESSION"); | |||
if (keycloakSession) { | |||
setCookie("KEYCLOAK_SESSION", keycloakSession, 1); | |||
} | |||
var keycloakIdentity = getCookie("KEYCLOAK_IDENTITY"); | |||
if (keycloakIdentity) { | |||
setCookie("KEYCLOAK_IDENTITY", keycloakIdentity, 1); | |||
} | |||
// 其他相关的 Cookie 同样处理 | |||
} | |||
// 读取特定 Cookie 的值 | |||
function getCookie(name) { | |||
var value = "; " + document.cookie; | |||
var parts = value.split("; " + name + "="); | |||
if (parts.length === 2) return parts.pop().split(";").shift(); | |||
} | |||
// 设置 Cookie | |||
function setCookie(name, value, days) { | |||
var expires = ""; | |||
if (days) { | |||
var date = new Date(); | |||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |||
expires = "; expires=" + date.toUTCString(); | |||
} | } | ||
} | document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=.biodiversity.cern.ac.cn; Secure; SameSite=None"; | ||
} | } | ||
}); | |||
}); | }); |
2024年6月22日 (六) 09:51的最新版本
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */ $(document).ready(function() { $('.gallery a.image').each(function() { var href = $(this).attr('href'); // 修改链接以指向直接的图片浏览器查看模式 $(this).attr('href', href + '?action=render'); }); // 更改Logo链接 var logoLink = document.querySelector('#p-logo .mw-wiki-logo'); if (logoLink) { logoLink.href = "https://www.biodiversity.cern.ac.cn"; } // 更改标题链接 var titleLink = document.querySelector('#p-logo .mw-wiki-title'); if (titleLink) { titleLink.href = "https://www.biodiversity.cern.ac.cn"; } // 加载 Keycloak JS 库并手动设置跨域 Cookie $.getScript('https://cdnjs.cloudflare.com/ajax/libs/keycloak-js/16.1.1/keycloak.min.js', function() { // 初始化 Keycloak const keycloak = new Keycloak({ url: 'https://sso.biodiversity.cern.ac.cn/auth', realm: 'biodiversity', clientId: 'wiki-client' }); keycloak.init({ onLoad: 'check-sso', promiseType: 'native' // Optional: Use native Promises for better compatibility }).then(function(authenticated) { if (authenticated) { console.log('Authenticated'); checkKeycloakLogin(); performMediaWikiLogin(); } else { console.log('Not authenticated'); } }).catch(function() { console.log('Failed to initialize Keycloak'); }); // Function to trigger MediaWiki login function performMediaWikiLogin() { const currentUrl = window.location.href; const loginUrl = 'https://wiki.biodiversity.cern.ac.cn/index.php/Special:PluggableAuthLogin'; if (!currentUrl.includes('Special:PluggableAuthLogin')) { window.location.href = loginUrl; } } // 检查 Keycloak 登录状态并设置 Cookie function checkKeycloakLogin() { var keycloakSession = getCookie("KEYCLOAK_SESSION"); if (keycloakSession) { setCookie("KEYCLOAK_SESSION", keycloakSession, 1); } var keycloakIdentity = getCookie("KEYCLOAK_IDENTITY"); if (keycloakIdentity) { setCookie("KEYCLOAK_IDENTITY", keycloakIdentity, 1); } // 其他相关的 Cookie 同样处理 } // 读取特定 Cookie 的值 function getCookie(name) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length === 2) return parts.pop().split(";").shift(); } // 设置 Cookie function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=.biodiversity.cern.ac.cn; Secure; SameSite=None"; } }); });