转到内容

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);
华夏公益教科书