轉到內容

MediaWiki:Gadget-collectionparser.js

來自華夏公益教科書,面向開放世界的開放式書籍
注意:在儲存後,更改可能不會立即生效。單擊此處詳細瞭解如何繞過瀏覽器的快取。
  • Mozilla / Firefox / Safari:在單擊“重新載入”的同時按住 Shift 鍵,或按 Ctrl-Shift-R (Apple Mac 上為 Cmd-Shift-R);
  • Internet Explorer:在單擊“重新整理”的同時按住 Ctrl 鍵,或按 Ctrl-F5
  • Konqueror:只需單擊“重新載入”按鈕,或按 F5
  • Opera 使用者可能需要在 工具 → 偏好設定 中徹底清除其快取。
// 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 = '{{saved book}}\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 += ":[[" + 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:", "");
	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);
華夏公益教科書