User:Gryllida/js/archive-talk.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: 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. — More skins

$('.mw-headline').each(function( index ) {
  // console.log( index + ": " + $( this ).text() );
  var txt = $( this ).text();
  if(mw.config.get( 'wgPageName' ) != 'User_talk:' + mw.config.get( 'wgUserName' )){
  	return;
  }
  //if(txt.indexOf('Wikinews recent additions') == -1){
  //	return true;
  //}
  var $self = $( this );
  // dom
  var $span = $('<span class="archive-talk">');
  $span.text(' Archive');
  $span.attr('style', 'color: blue');
  
  $span_y = $("<span class='archive-talk' style='color:blue'>Yes</span>");
  $span_n = $("<span class='archive-talk' style='color:blue'>No</span>");
  
  var $span2 = $("<span class='archive-talk'> Are you sure? </span>");
  $span2.append($span_y );
  $span2.append($('<span> / </span>') );
  $span2.append($span_n );

  // event
  $span.click(function() {    $span2.show();    $(this).hide();  });
  $span_n.click(function() {    $span2.hide();    $span.show();  });
  // // delete the section with number 'index - 1' via api
  $span_y.click(function() {  
  	
  	var api = new mw.Api();
  	// get section contents
  	// https://en.wikipedia.org/w/api.php?action=parse&page=New_York&format=json&prop=wikitext&section=1&format=json
  	api.get( {
    		action: 'parse',
    		page: mw.config.get( 'wgPageName' ),
    		prop: 'wikitext',
    		section: index+1
		} ).done( function ( data ) {
			text = data.parse.wikitext['*'];
			text = '\r\n' + text;
			// add section contents to '/Archive' page
			mytitle = mw.config.get ('wgPageName') + '/Archive';
			api.postWithToken("edit", {
				action: 'edit',
				title: mytitle,
				summary: 'archiving section ([[User:Gryllida/js/archive-talk.js|assisted]])',
				appendtext: text
			});
		});
  	
  	// blank the section
  	api.postWithToken("edit", {
      action: 'edit',
      title: mw.config.get ('wgPageName'),
      section: index+1,
      summary: 'archiving section ([[User:Gryllida/js/archive-talk.js|assisted]])',
      text: ''
  	});
  	// update the view
  	var $anchor = $self.parent();
  	$anchor.nextUntil( "h2" ).remove();
  	$anchor.remove();
  	$('.archive-talk').remove();
  	// importScript('User:Gryllida/js/archive-talk.js'); // does not work in global.js
  	mw.loader.load( 'https://en.wikinews.org/w/index.php?title=User:Gryllida/js/archive-talk.js&action=raw&ctype=text/javascript' ); // [[:n:User:Gryllida/js/archive-talk.js]]
  });
  
  // add to h
  $span.insertAfter($(this));
  $span2.insertAfter($(this));
  $span2.hide();
});