MediaWiki:Gadget-collectionparser.js
Appearance
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
// Adapted from User:Pediapress/collection-parser.js
var bookURL = $.escapeSelector(mw.config.get('wgServer') + mw.util.getUrl( mw.config.get( 'wgBookName' )));
var pathRE;
function putOutList(list) {
var pagename, bookname = list[0].replace(/_/g, " ");
var out = '{{saved book}}\n' + '== ' + bookname +' ==\n:[[' + bookname + ']]\n';
pathRE = new RegExp( '^https?:' + bookURL + '\\/' );
list.shift();
$.each( list, function(i) {
if ( this.match(pathRE) ) {
pagename = this.replace(pathRE, '').replace(/_/g, " ");
out += ":[[" + bookname + "/" + pagename + "|" + pagename + "]]\n";
} else if ( list[i+1] && list[i+1].match(pathRE) && $.trim(this) !== "" ) {
// kick out empty headlines and those without links
out += ";" + this + "\n";
}
});
out += "\n[[Category:Collections]]";
// print the collectionmarkup into a textarea
var collectionName = prompt(`Name your collection (default: ${bookname})`, bookname);
if (collectionName) {
var w = window.open(mw.config.get('wgServer') + "/w/index.php?title=User:" + mw.config.get('wgUserName') + "/Collections/" + collectionName + "&action=edit");
$(w).on( 'load', function() { refreshTextArea(w, out); } );
}
else return;
}
function refreshTextArea(w, out) {
var txt = w.document.getElementById('wpTextbox1');
txt.value = out;
}
// look for headlines and links
function parseContent ($content) {
var bookLinks = [mw.config.get( 'wgBookName' )];
var pathRE;
pathRE = new RegExp( '^https?:' + bookURL + '$|^https?:' + bookURL + '\\/' );
$.each($content.find(".mw-heading :header, a"), function() {
var $this = $(this), val;
if ( $this.is('.mw-heading') ) {
bookLinks[bookLinks.length] = $this.html() || 'chapter';
} else if ( $this.is('a') && this.href.match(pathRE) && !this.href.match(/#/) ) {
bookLinks[bookLinks.length] = this.href;
}
});
return bookLinks;
}
// added link has been clicked
function getCollection($content) {
putOutList(parseContent($content));
}
const showLink = mw.config.get("wgArticleId") > 0 && !mw.config.get("wgIsMainPage") && (mw.config.get('wgNamespaceNumber') == 0 || mw.config.get('wgNamespaceNumber') == 102 || mw.config.get('wgNamespaceNumber') == 110);
var collectionParserPortletLink = showLink ? mw.util.addPortletLink('p-personal', '#', 'Get collection', 'pediapress') : null;
// add a link at the right upper corner
function addPediapressLinks($content) {
if (showLink) {
$(collectionParserPortletLink).click(function() {
getCollection($content);
});
}
}
mw.hook('wikipage.content').add(addPediapressLinks);