MediaWiki:Gadget-collectionparser.js

// Adapted from User:Pediapress/collection-parser.js

function putOutList(list) { var bookURL = mw.config.get('wgServer') + mw.util.getUrl( mw.config.get( 'wgBookName' ) ); var pathRE; var pagename, bookname = list[0].replace(/_/g, " "); var out = '\n' + '== ' + bookname +' ==\n:' + bookname + '\n'; bookURL = $.escapeRE(bookURL); pathRE = new RegExp( '^https?:' + bookURL + '\\/' ); list.shift; $.each( list, function(i) {		if ( this.match(pathRE) ) {			pagename = this.replace(pathRE, '').replace(/_/g, " ");			out += ":" + 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";

// print the collectionmarkup into a textarea var collectionName = prompt("Name your collection:", ""); 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); } ); } } function refreshTextArea(w, out) { var txt = w.document.getElementById('wpTextbox1'); txt.value = out; }

// look for headlines and links function parseContent ($content) { var bookURL = mw.config.get('wgServer') + mw.util.getUrl( mw.config.get( 'wgBookName' ) ); var bookLinks = [mw.config.get( 'wgBookName' )]; var pathRE; bookURL = $.escapeRE(bookURL); pathRE = new RegExp( '^https?:' + bookURL + '$|^https?:' + bookURL + '\\/' ); $.each( $content.find(".mw-headline, a"), function {		var $this = $(this), val;		if ( $this.is('.mw-headline') ) {			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)); } // add an link at thr right upper corner function addPediapressLinks($content) { if ( $.inArray( mw.config.get('wgNamespaceNumber'), [ 0, 102, 110 ] ) !== -1 ) { $(mw.util.addPortletLink ('p-personal', '#', 'get collection')).click(function {			getCollection($content);		}); } }

mw.hook('wikipage.content').add(addPediapressLinks);