No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/** | /** | ||
* reFill.js — | * reFill.js — BharatpediaNeo-safe | ||
* | * Fixed "Fill bare URLs" button + Citoid batch fill. | ||
*/ | */ | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
function ready(fn) { | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', fn); | |||
} else { | |||
fn(); | |||
} | |||
} | |||
function getTextarea() { | |||
return document.querySelector('#wpTextbox1') || | |||
document.querySelector('#editform textarea') || | |||
document.querySelector('.mw-editform textarea') || | |||
document.querySelector('textarea.ve-ce-documentNode') || | |||
document.querySelector('#mw-content-text textarea') || | |||
document.querySelector('textarea'); | |||
} | } | ||
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/'; | var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/'; | ||
var TYPE_MAP = { | var TYPE_MAP = { | ||
webpage: 'Cite web', | |||
blogPost: 'Cite web', | blogPost: 'Cite web', | ||
newspaperArticle: 'Cite news', | |||
magazineArticle: 'Cite news', | |||
journalArticle: 'Cite journal', | |||
conferencePaper: 'Cite journal', | |||
book: 'Cite book', | book: 'Cite book', | ||
manuscript: 'Cite book', | manuscript: 'Cite book', | ||
_default: 'Cite web' | _default: 'Cite web' | ||
}; | }; | ||
| Line 58: | Line 40: | ||
} | } | ||
function templateFromCitoid(item, url) { | |||
function templateFromCitoid(item, | |||
var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default; | var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default; | ||
var p = [ | var p = ['url=' + esc((item && item.url) || url)]; | ||
if (item && item.title) { | if (item && item.title) { | ||
p.push('title=' + esc(item.title)); | p.push('title=' + esc(item.title)); | ||
| Line 74: | Line 48: | ||
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher); | var site = item && (item.websiteTitle || item.publicationTitle || item.publisher); | ||
if (site) { | if (site) { | ||
p.push((name === 'Cite news' || name === 'Cite journal' ? 'work=' : 'website=') + esc(site)); | |||
} | } | ||
if (item && item.date) { | if (item && item.date) { | ||
p.push('date=' + esc(item.date)); | p.push('date=' + esc(item.date)); | ||
} | } | ||
p.push('access-date=' + new Date().toISOString().slice(0, 10)); | |||
return '{{' + name + '|' + p.join('|') + '}}'; | return '{{' + name + '|' + p.join('|') + '}}'; | ||
} | } | ||
function findBare( | function findBare(text) { | ||
var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi; | var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi; | ||
var out = [] | var out = [], m; | ||
while ((m = re.exec(text)) !== null) { | |||
while ((m = re.exec( | out.push({ full: m[0], attrs: m[1] || '', url: m[2] }); | ||
out.push({ | |||
} | } | ||
return out; | return out; | ||
} | } | ||
function fetchOne(url) { | function fetchOne(url) { | ||
return fetch(CITOID + encodeURIComponent(url), { | return fetch(CITOID + encodeURIComponent(url), { headers: { Accept: 'application/json' } }) | ||
.then(function (r) { | |||
if (!r.ok) { | |||
throw new Error(String(r.status)); | |||
} | |||
return r.json(); | |||
}) | |||
.then(function (data) { | |||
return Array.isArray(data) ? data[0] : data; | |||
}); | |||
} | } | ||
function run() { | function run() { | ||
var | var ta = getTextarea(); | ||
if (! | if (!ta) { | ||
window.alert('reFill: no editor textarea. Use Edit source (?action=edit).'); | |||
return; | return; | ||
} | } | ||
var text = ta.value; | |||
var text = | |||
var bare = findBare(text); | var bare = findBare(text); | ||
if (!bare.length) { | if (!bare.length) { | ||
window.alert('reFill: no <ref>https://…</ref> found.'); | |||
return; | return; | ||
} | } | ||
var btn = document.getElementById('refill-bare-btn'); | |||
var | if (btn) { | ||
btn.disabled = true; | |||
btn.textContent = 'Filling ' + bare.length + '…'; | |||
} | |||
var chain = Promise.resolve(); | var chain = Promise.resolve(); | ||
var ok = 0; | var ok = 0; | ||
bare.forEach(function (item) { | bare.forEach(function (item) { | ||
chain = chain.then(function () { | chain = chain.then(function () { | ||
return fetchOne(item.url).then(function (meta) { | return fetchOne(item.url).then(function (meta) { | ||
text = text.replace(item.full, '<ref' + item.attrs + '>' + templateFromCitoid(meta, item.url) + '</ref>'); | |||
ok++; | ok++; | ||
}).catch(function ( | }).catch(function () { /* leave ref */ }).then(function () { | ||
return new Promise(function (res) { setTimeout(res, 350); }); | |||
return new Promise(function ( | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
chain.then(function () { | chain.then(function () { | ||
ta.value = text; | |||
ta.dispatchEvent(new Event('input', { bubbles: true })); | |||
if (btn) { | |||
btn.disabled = false; | |||
btn.textContent = 'Fill bare URLs'; | |||
} | } | ||
window.alert('reFill finished: ' + ok + '/' + bare.length + '. Preview before saving.'); | |||
}); | }); | ||
} | } | ||
function addButton() { | function addButton() { | ||
if ( | if (document.getElementById('refill-bare-btn')) { | ||
return | return; | ||
} | } | ||
var btn = document.createElement('button'); | |||
var | btn.id = 'refill-bare-btn'; | ||
btn.type = 'button'; | |||
btn.textContent = 'Fill bare URLs'; | |||
btn.style.cssText = [ | |||
'position:fixed', | |||
'right:16px', | |||
cursor: | 'bottom:16px', | ||
'z-index:2147483647', | |||
'padding:10px 14px', | |||
'font:600 14px/1.2 sans-serif', | |||
'color:#fff', | |||
'background:#b32424', | |||
'border:0', | |||
'border-radius:6px', | |||
'cursor:pointer', | |||
'box-shadow:0 2px 8px rgba(0,0,0,.35)' | |||
].join(';'); | |||
btn.addEventListener('click', function (e) { | |||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | |||
run(); | run(); | ||
}); | }); | ||
document.body.appendChild(btn); | |||
} | } | ||
ready(function () { | |||
addButton(); | addButton(); | ||
setTimeout(addButton, | setTimeout(addButton, 500); | ||
setTimeout(addButton, 2000); | setTimeout(addButton, 2000); | ||
setTimeout(addButton, | setTimeout(addButton, 5000); | ||
}); | }); | ||
})(); | })(); | ||
Revision as of 10:49, 25 September 2026
/**
* reFill.js — BharatpediaNeo-safe
* Fixed "Fill bare URLs" button + Citoid batch fill.
*/
(function () {
'use strict';
function ready(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
function getTextarea() {
return document.querySelector('#wpTextbox1') ||
document.querySelector('#editform textarea') ||
document.querySelector('.mw-editform textarea') ||
document.querySelector('textarea.ve-ce-documentNode') ||
document.querySelector('#mw-content-text textarea') ||
document.querySelector('textarea');
}
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
var TYPE_MAP = {
webpage: 'Cite web',
blogPost: 'Cite web',
newspaperArticle: 'Cite news',
magazineArticle: 'Cite news',
journalArticle: 'Cite journal',
conferencePaper: 'Cite journal',
book: 'Cite book',
manuscript: 'Cite book',
_default: 'Cite web'
};
function esc(s) {
return String(s || '').replace(/\|/g, '{{!}}').replace(/\n/g, ' ').trim();
}
function templateFromCitoid(item, url) {
var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default;
var p = ['url=' + esc((item && item.url) || url)];
if (item && item.title) {
p.push('title=' + esc(item.title));
}
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher);
if (site) {
p.push((name === 'Cite news' || name === 'Cite journal' ? 'work=' : 'website=') + esc(site));
}
if (item && item.date) {
p.push('date=' + esc(item.date));
}
p.push('access-date=' + new Date().toISOString().slice(0, 10));
return '{{' + name + '|' + p.join('|') + '}}';
}
function findBare(text) {
var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi;
var out = [], m;
while ((m = re.exec(text)) !== null) {
out.push({ full: m[0], attrs: m[1] || '', url: m[2] });
}
return out;
}
function fetchOne(url) {
return fetch(CITOID + encodeURIComponent(url), { headers: { Accept: 'application/json' } })
.then(function (r) {
if (!r.ok) {
throw new Error(String(r.status));
}
return r.json();
})
.then(function (data) {
return Array.isArray(data) ? data[0] : data;
});
}
function run() {
var ta = getTextarea();
if (!ta) {
window.alert('reFill: no editor textarea. Use Edit source (?action=edit).');
return;
}
var text = ta.value;
var bare = findBare(text);
if (!bare.length) {
window.alert('reFill: no <ref>https://…</ref> found.');
return;
}
var btn = document.getElementById('refill-bare-btn');
if (btn) {
btn.disabled = true;
btn.textContent = 'Filling ' + bare.length + '…';
}
var chain = Promise.resolve();
var ok = 0;
bare.forEach(function (item) {
chain = chain.then(function () {
return fetchOne(item.url).then(function (meta) {
text = text.replace(item.full, '<ref' + item.attrs + '>' + templateFromCitoid(meta, item.url) + '</ref>');
ok++;
}).catch(function () { /* leave ref */ }).then(function () {
return new Promise(function (res) { setTimeout(res, 350); });
});
});
});
chain.then(function () {
ta.value = text;
ta.dispatchEvent(new Event('input', { bubbles: true }));
if (btn) {
btn.disabled = false;
btn.textContent = 'Fill bare URLs';
}
window.alert('reFill finished: ' + ok + '/' + bare.length + '. Preview before saving.');
});
}
function addButton() {
if (document.getElementById('refill-bare-btn')) {
return;
}
var btn = document.createElement('button');
btn.id = 'refill-bare-btn';
btn.type = 'button';
btn.textContent = 'Fill bare URLs';
btn.style.cssText = [
'position:fixed',
'right:16px',
'bottom:16px',
'z-index:2147483647',
'padding:10px 14px',
'font:600 14px/1.2 sans-serif',
'color:#fff',
'background:#b32424',
'border:0',
'border-radius:6px',
'cursor:pointer',
'box-shadow:0 2px 8px rgba(0,0,0,.35)'
].join(';');
btn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
run();
});
document.body.appendChild(btn);
}
ready(function () {
addButton();
setTimeout(addButton, 500);
setTimeout(addButton, 2000);
setTimeout(addButton, 5000);
});
})();