MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
(Created page with "→Any JavaScript here will be loaded for all users on every page load.: // Redirect top Edit tab to Page Forms edit URL on college pages mw.hook('wikipage.content').add(function () { var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]'); var editTab = document.querySelector('#ca-edit a'); if (formButton && editTab) { editTab.href = formButton.href; editTab.textContent = 'Edit'; } });") |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
/ | // Honour Wiki: force CollegePage editing through Page Forms | ||
mw. | (function () { | ||
function currentTitle() { | |||
return mw.config.get('wgPageName'); | |||
} | |||
function isLoggedIn() { | |||
return !!mw.config.get('wgUserName'); | |||
} | |||
function formEditUrl(title, focusField) { | |||
var url = mw.util.getUrl('Special:FormEdit/CollegePage/' + title); | |||
if (focusField) { | |||
url += '?pf_focus=' + encodeURIComponent(focusField); | |||
} | |||
return url; | |||
} | |||
function loginUrl() { | |||
return mw.util.getUrl('Special:UserLogin', { | |||
returnto: currentTitle() | |||
}); | |||
} | |||
function showLoginBox() { | |||
var box = document.querySelector('.honour-college-edit-box'); | |||
if (!box || isLoggedIn()) return; | |||
box.innerHTML = | |||
'<div style="font-size:18px;font-weight:700;margin-bottom:8px;">Login required to edit</div>' + | |||
'<div style="margin-bottom:12px;">Only logged-in students can update college information.</div>' + | |||
'<a href="' + loginUrl() + '" style="display:inline-block;padding:10px 16px;border:1px solid #3366cc;border-radius:6px;background:#3366cc;color:#fff;text-decoration:none;font-weight:600;">Login to edit</a>'; | |||
} | |||
function rewriteMainEditTab() { | |||
var editTab = document.querySelector('#ca-edit a'); | |||
var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]'); | |||
if (!editTab) return; | |||
if (!isLoggedIn()) { | |||
editTab.href = loginUrl(); | |||
editTab.textContent = 'Login to edit'; | |||
return; | |||
} | |||
if (formButton) { | |||
editTab.href = formEditUrl(currentTitle()); | |||
editTab.textContent = 'Edit'; | |||
} | |||
} | |||
function sectionToField(sectionText) { | |||
sectionText = (sectionText || '').toLowerCase(); | |||
if (sectionText.indexOf('overview') !== -1) return 'overview'; | |||
if (sectionText.indexOf('history') !== -1) return 'history'; | |||
if (sectionText.indexOf('programs') !== -1) return 'programs'; | |||
if (sectionText.indexOf('facilities') !== -1) return 'facilities'; | |||
if (sectionText.indexOf('admissions') !== -1) return 'admissions'; | |||
if (sectionText.indexOf('affiliations') !== -1) return 'affiliations'; | |||
if (sectionText.indexOf('alumni') !== -1) return 'alumni'; | |||
if (sectionText.indexOf('references') !== -1) return 'references'; | |||
return ''; | |||
} | |||
function rewriteSectionEditLinks() { | |||
var sectionLinks = document.querySelectorAll('.mw-editsection a'); | |||
sectionLinks.forEach(function (link) { | |||
var heading = link.closest('.mw-heading, h2, h3, h4'); | |||
var headingText = heading ? heading.textContent : ''; | |||
var field = sectionToField(headingText); | |||
if (!field) return; | |||
if (!isLoggedIn()) { | |||
link.href = loginUrl(); | |||
link.textContent = 'login to edit'; | |||
} else { | |||
link.href = formEditUrl(currentTitle(), field); | |||
link.textContent = 'edit'; | |||
} | |||
}); | |||
} | |||
function redirectRawCollegeEdit() { | |||
if (mw.config.get('wgAction') !== 'edit') return; | |||
var textarea = document.querySelector('#wpTextbox1'); | |||
if (!textarea) return; | |||
if (textarea.value.indexOf('{{CollegePage') !== -1) { | |||
window.location.href = formEditUrl(currentTitle()); | |||
} | |||
} | |||
function focusFormField() { | |||
var params = new URLSearchParams(window.location.search); | |||
var field = params.get('pf_focus'); | |||
if (!field) return; | |||
setTimeout(function () { | |||
var selectors = [ | |||
'[name*="[' + field + ']"]', | |||
'[name="' + field + '"]', | |||
'textarea[name*="' + field + '"]', | |||
'input[name*="' + field + '"]', | |||
'#input_' + field | |||
]; | |||
for (var i = 0; i < selectors.length; i++) { | |||
var el = document.querySelector(selectors[i]); | |||
if (el) { | |||
el.scrollIntoView({ behavior: 'smooth', block: 'center' }); | |||
el.focus(); | |||
return; | |||
} | |||
} | |||
}, 500); | |||
} | } | ||
}); | $(function () { | ||
showLoginBox(); | |||
rewriteMainEditTab(); | |||
rewriteSectionEditLinks(); | |||
redirectRawCollegeEdit(); | |||
focusFormField(); | |||
}); | |||
})(); | |||
Latest revision as of 18:10, 16 May 2026
// Honour Wiki: force CollegePage editing through Page Forms
(function () {
function currentTitle() {
return mw.config.get('wgPageName');
}
function isLoggedIn() {
return !!mw.config.get('wgUserName');
}
function formEditUrl(title, focusField) {
var url = mw.util.getUrl('Special:FormEdit/CollegePage/' + title);
if (focusField) {
url += '?pf_focus=' + encodeURIComponent(focusField);
}
return url;
}
function loginUrl() {
return mw.util.getUrl('Special:UserLogin', {
returnto: currentTitle()
});
}
function showLoginBox() {
var box = document.querySelector('.honour-college-edit-box');
if (!box || isLoggedIn()) return;
box.innerHTML =
'<div style="font-size:18px;font-weight:700;margin-bottom:8px;">Login required to edit</div>' +
'<div style="margin-bottom:12px;">Only logged-in students can update college information.</div>' +
'<a href="' + loginUrl() + '" style="display:inline-block;padding:10px 16px;border:1px solid #3366cc;border-radius:6px;background:#3366cc;color:#fff;text-decoration:none;font-weight:600;">Login to edit</a>';
}
function rewriteMainEditTab() {
var editTab = document.querySelector('#ca-edit a');
var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]');
if (!editTab) return;
if (!isLoggedIn()) {
editTab.href = loginUrl();
editTab.textContent = 'Login to edit';
return;
}
if (formButton) {
editTab.href = formEditUrl(currentTitle());
editTab.textContent = 'Edit';
}
}
function sectionToField(sectionText) {
sectionText = (sectionText || '').toLowerCase();
if (sectionText.indexOf('overview') !== -1) return 'overview';
if (sectionText.indexOf('history') !== -1) return 'history';
if (sectionText.indexOf('programs') !== -1) return 'programs';
if (sectionText.indexOf('facilities') !== -1) return 'facilities';
if (sectionText.indexOf('admissions') !== -1) return 'admissions';
if (sectionText.indexOf('affiliations') !== -1) return 'affiliations';
if (sectionText.indexOf('alumni') !== -1) return 'alumni';
if (sectionText.indexOf('references') !== -1) return 'references';
return '';
}
function rewriteSectionEditLinks() {
var sectionLinks = document.querySelectorAll('.mw-editsection a');
sectionLinks.forEach(function (link) {
var heading = link.closest('.mw-heading, h2, h3, h4');
var headingText = heading ? heading.textContent : '';
var field = sectionToField(headingText);
if (!field) return;
if (!isLoggedIn()) {
link.href = loginUrl();
link.textContent = 'login to edit';
} else {
link.href = formEditUrl(currentTitle(), field);
link.textContent = 'edit';
}
});
}
function redirectRawCollegeEdit() {
if (mw.config.get('wgAction') !== 'edit') return;
var textarea = document.querySelector('#wpTextbox1');
if (!textarea) return;
if (textarea.value.indexOf('{{CollegePage') !== -1) {
window.location.href = formEditUrl(currentTitle());
}
}
function focusFormField() {
var params = new URLSearchParams(window.location.search);
var field = params.get('pf_focus');
if (!field) return;
setTimeout(function () {
var selectors = [
'[name*="[' + field + ']"]',
'[name="' + field + '"]',
'textarea[name*="' + field + '"]',
'input[name*="' + field + '"]',
'#input_' + field
];
for (var i = 0; i < selectors.length; i++) {
var el = document.querySelector(selectors[i]);
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
el.focus();
return;
}
}
}, 500);
}
$(function () {
showLoginBox();
rewriteMainEditTab();
rewriteSectionEditLinks();
redirectRawCollegeEdit();
focusFormField();
});
})();