Ծանուցում. Հիշելուց հետո կատարված փոփոխությունները տեսնելու համար մաքրեք ձեր զննարկիչի հիշապահեստը. Mozilla / Firefox / Safari՝ Ctrl+Shift+R (Cmd+Shift+R Mac OS X-ում) Konqueror՝ F5 Opera՝ Tools→Preferences ընտրացանկից։ Internet Explorer՝ Ctrl+F5

$.when( $.ready ).then( function () {
	var isTitleRequired = false, isClicked = false,
		fApi, restore, otherHTML, otherWikicode;
	if ( $( '.interlanguage-link' ).length === 0 ) {
		isTitleRequired = true;
	}
	function sidebarHide() {
		document.getElementById( 'mw-panel' ).style.visibility = 'hidden';
		document.getElementById( 'mw-head-base' ).style.marginLeft = '0';
		document.getElementById( 'content' ).style.marginLeft = '0';
		document.getElementById( 'left-navigation' ).style.marginLeft = '0';
		document.getElementById( 'footer' ).style.marginLeft = '0';
	}

	function sidebarShow() {
		document.getElementById( 'mw-panel' ).style.visibility = '';
		document.getElementById( 'mw-head-base' ).style.marginLeft = '';
		document.getElementById( 'content' ).style.marginLeft = '';
		document.getElementById( 'left-navigation' ).style.marginLeft = '';
		document.getElementById( 'footer' ).style.marginLeft = '';
	}
	$( '#p-lang-label' ).after( '<div><input type="text" id="comp-lang" style="width: 20px;"/><br><input type="text" id="comp-other-title" style="width: 80%;"/><br><button id="comp-start" type="button">Համեմատել</button></div>' );
	if ( isTitleRequired ) {
		$( '#comp-other-title' ).attr( 'placeholder', 'Պարտադիր է' );
		$( '#comp-other-title' ).prop( 'required', true );
	} else {
		$( '#comp-other-title' ).attr( 'placeholder', 'Պարտադիր չէ' );
	}

	$( '#comp-start' ).on( 'click', function () {
		var myRegexp = /https:\/\/([^.]+)\.wikipedia\.org\/wiki\/(.+)/,
			otherLang = $( '#comp-lang' ).val(), $otherLink, otherTitle, match, isTranslated = false, isFetchingLinks = false;
		if ( isClicked ) {
			return;
		}
		isClicked = true;
		if ( $( '#comp-other-title' ).val().trim() ) {
			otherTitle = $( '#comp-other-title' ).val().trim();
		} else {
			$otherLink = $( '.interwiki-' + $( '#comp-lang' ).val() );
			if ( $otherLink ) {
				match = myRegexp.exec( $( $otherLink[ 0 ] ).find( 'a' )[ 0 ].href );
				otherTitle = match[ 2 ];
			}
		}
		$( 'html' ).css( 'cursor', 'progress' );
		if ( otherTitle && otherLang ) {
			fApi = new mw.ForeignApi( 'https://' + otherLang + '.wikipedia.org/w/api.php' );
			fApi.get( {
				action: 'parse',
				page: decodeURIComponent( otherTitle ),
				prop: 'text',
				formatversion: 2,
				format: 'json'
			} ).then( function ( HTML ) {
				var $HTML = $( HTML.parse.text.replace( /cite_note/g, 'comp_other_cite_note' ) );
				$HTML.find( 'a' ).each( function () {
					if ( this.href.indexOf( '#' ) === -1 ) {
						$( this ).attr( 'href', this.href.replace( 'hy', otherLang ) );
					}
				} );
				fApi.get( {
					action: 'parse',
					page: decodeURIComponent( otherTitle ),
					prop: 'wikitext',
					formatversion: 2,
					format: 'json'
				} ).then( function ( wikicode ) {
					var transWikitext;
					$( 'html' ).scrollTop( 0 );
					$( 'html' ).css( 'cursor', 'default' );
					sidebarHide();
					$( '#content' ).wrap( '<div style="width: 50%; height: 100vh; overflow: scroll; float:right;" id="comp-right"><div style="padding: 0px;"></div></div>' );
					$( '#comp-right' ).before( '<div style="width: 50%; height: 100vh;overflow: scroll; float:left; position: relative;" id="comp-left"><h1><a href="https://' + otherLang + '.wikipedia.org/wiki/' + decodeURIComponent( otherTitle ) + '">' + decodeURIComponent( otherTitle ).replace( /_/g, ' ' ) + '</a></h1><span id="langComp-dummy" style="display:none">0</span><div id="comp-HTML" style="padding: 15px;background-color: #ffffff; color: #202122;">' + $HTML.html() + '</div><textarea id="comp-Wikicode" style="padding: 15px;width: 100%;height: calc(100% - 45px);box-sizing: border-box;" readonly></textarea></div>' );
					$( '#comp-Wikicode' ).text( wikicode.parse.wikitext );
					if ( !restore ) {
						restore = mw.util.addPortletLink( 'p-cactions', '#', 'Ավարտել համեմատումը', 'ca-sidebar', 'Փակել այլ լեզվի պատուհանը և վերականգներ սկզբնական տեսք' );
					}
					$( '#comp-HTML' ).show();
					$( '#comp-Wikicode' ).hide();
					otherHTML = mw.util.addPortletLink( 'p-namespaces', '#', 'Կարդալ այլ լեզվով', 'p-otherHTML', 'Կարդալ հոդվածի այլ լեզվով տարբերակը' );
					$( otherHTML ).addClass( 'selected' );
					otherWikicode = mw.util.addPortletLink( 'p-namespaces', '#', 'Տեսնել կոդը', 'p-otherWikicode', 'Տեսնել հոդվածի այլ լեզվով կոդը' );
					$( '#ca-ve-edit' ).on( 'click', function () {
						$( '#comp-right' ).scrollTop( 0 );
					} );
					$( $( '<a>' ).attr( { id: 'langComp-trans', href: '#' } )
						.text( 'Թարգմանել հղումները' )
						.css( 'font-size', '60%' )
						.hide() ).insertAfter( $( '#p-otherWikicode' ) );
					$( otherHTML ).on( 'click', function ( e ) {
						e.preventDefault();
						$( '#langComp-trans' ).hide();
						$( this ).addClass( 'selected' );
						$( otherWikicode ).removeClass( 'selected' );
						$( '#comp-HTML' ).show();
						$( '#comp-Wikicode' ).hide();
					} );
					$( otherWikicode ).on( 'click', function ( e ) {
						e.preventDefault();
						$( '#langComp-trans' ).show();
						$( this ).addClass( 'selected' );
						$( otherHTML ).removeClass( 'selected' );
						$( '#comp-HTML' ).hide();
						$( '#comp-Wikicode' ).show();
					} );
					$( restore ).on( 'click', function ( e ) {
						e.preventDefault();
						transWikitext = undefined;
						$( '#langComp-trans' ).hide();
						sidebarShow();
						isClicked = false;
						$( '#content' ).unwrap().unwrap();
						$( '#comp-left' ).remove();
						$( '#langComp-trans' ).remove();
						$( otherHTML ).remove();
						$( otherWikicode ).remove();
					} );
					$( '#langComp-trans' ).on( 'click', function ( e ) {
						var wbApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php' ), obj = {}, linkRegexp = /\[\[ *([^|\]]+)/g, $span = $( '#langComp-dummy' );
						e.preventDefault();
						if ( isFetchingLinks ) {
							return;
						}
						if ( isTranslated ) {
							isTranslated = !isTranslated;
							$( '#langComp-trans' ).text( 'Թարգմանել հղումները' );
							$( '#comp-Wikicode' ).text( wikicode.parse.wikitext );
							return;
						} else if ( transWikitext ) {
							isTranslated = !isTranslated;
							$( '#langComp-trans' ).text( 'Վերականգնել հղումները' );
							$( '#comp-Wikicode' ).text( transWikitext );
							return;
						}
						isTranslated = !isTranslated;
						isFetchingLinks = true;
						function findlink( link ) {
							fApi.get( {
								action: 'query',
								titles: link,
								redirects: true,
								formatversion: '2'
							} ).then( function ( red ) {
								var title = link;
								if ( red && red.query && red.query.pages && red.query.pages[ 0 ] && red.query.pages[ 0 ].title ) {
									title = red.query.pages[ 0 ].title;
								}
								wbApi.get( {
									action: 'wbgetentities',
									format: 'json',
									sites: otherLang + 'wiki',
									titles: title,
									props: 'sitelinks',
									languages: 'hy',
									sitefilter: 'hywiki',
									formatversion: '2'
								} ).then( function ( data ) {
									var Q = data.entities[ Object.keys( data.entities )[ 0 ] ];
									$span.text( parseInt( $span.text() ) - 1 );
									if ( data.success && Q.sitelinks && Q.sitelinks.hywiki ) {
										obj[ link ] = Q.sitelinks.hywiki.title;
									}
								}, function () {
									$span.text( parseInt( $span.text() ) - 1 );
								}
								);
							}, function () {
								$span.text( parseInt( $span.text() ) - 1 );
							} );
						}
						match = linkRegexp.exec( wikicode.parse.wikitext );
						while ( match !== null ) {
							$span.text( parseInt( $span.text() ) + 1 );
							findlink( match[ 1 ].charAt( 0 ).toUpperCase() + match[ 1 ].slice( 1 ) );
							match = linkRegexp.exec( wikicode.parse.wikitext );
						}
						$span.on( 'DOMSubtreeModified', function () {
							if ( parseInt( $span.text() ) === 0 ) {
								transWikitext = wikicode.parse.wikitext.replace( linkRegexp, function ( m, c ) {
									if ( obj[ c ] ) {
										return '[[' + obj[ c ];
									} else if ( obj[ c.charAt( 0 ).toUpperCase() + c.slice( 1 ) ] ) {
										return '[[' + obj[ c.charAt( 0 ).toUpperCase() + c.slice( 1 ) ];
									} else {
										return m;
									}
								} );
								$( '#comp-Wikicode' ).text( transWikitext );
								$( '#langComp-trans' ).text( 'Վերականգնել հղումները' );
								isFetchingLinks = false;
							}
						} );
					} );
				}, function () {
					$( 'html' ).css( 'cursor', 'default' );
				} );
			}, function () {
				$( 'html' ).css( 'cursor', 'default' );
			} );
		} else {
			$( 'html' ).css( 'cursor', 'default' );
		}
	} );
} );