Jump to content

User:Technical 13/Scripts/CVD.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Technical 13 (talk | contribs) at 17:07, 26 June 2014 (This should fix the report for cases where there are no URLs after filtering. Also, this should fix the issue with duplicate links that only have a trailing '/' difference.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* Get links from page */
var externalLinks = [];
$('a.external').each(function(){
	externalLinks[externalLinks.length] = $(this).attr('href');
});
/* Clean out the duplicates */
externalLinks.sort();
var uniqueLinks = [];
$.each( externalLinks, function( i, el ){
	if($.inArray(el, uniqueLinks) === -1 && $.inArray(el + "/", uniqueLinks) === -1) uniqueLinks.push(el);
});
/* Filter out the invalid ones */
var uniqueURLs = [];
$.each( uniqueLinks, function( i, el ){
	if(
/* Internal WMF links			*/
	el.search(/\/\/(enwp|[\w]{2}\.wikipedia)\.org\/(w|wiki)?(\/index.php\?)?/i) === -1 &&/* Wikipedias */
	el.search(/\/\/tools(erver|\.wmflabs)\.org\/?/i) === -1 &&/* Tools and Labs */
/* Google and Bing searches		*/
	el.search(/\/\/(scholar|www).(bing|google).com\/(images|scholar|search|webhp)\?([\w_#&]*)?=/i) === -1 &&
/* jstor search				*/
	el.search(/\/\/www\.jstor\.org\/action\/doBasicSearch\?Query=/i) === -1 &&
/* Webchat				*/
	el.search(/\/\/webchat.freenode.net\/?/i) === -1
	){
		if(el.search(/https?:/i) === -1){
			el= 'http:' + el;
		}
		uniqueURLs.push(el);
	}
});

/* TRON report */

function lpad(n, e, d) {
  var o = ''; if(typeof(d) === 'undefined'){ d='0'; } if(typeof(e) === 'undefined'){ e=2; }
  if(n.length < e){ for(var r=0; r < e - n.length; r++){ o += d; } o += n; } else { o=n; }
  return o;
}
var loadTS = new Date;
loadTS = lpad(loadTS.getUTCFullYear().toString(), 4) + lpad((loadTS.getUTCMonth()+1).toString(), 2) + lpad(loadTS.getUTCDate().toString(), 2) + lpad(loadTS.getUTCHours().toString(), 2) + lpad(loadTS.getUTCMinutes().toString(), 2) + lpad(loadTS.getUTCSeconds().toString(), 2);
var taskBUG = '== [[User:Technical 13/Scripts/CVD.js|CVD]] bug report for ' + mw.config.get('wgPageName') + ' by ' +  mw.config.get('wgUserName').replace(/ /g,'_') + ' ==\n';
taskBUG += '{'+'{User8|' + mw.config.get('wgUserName') + '}} <sup>([[Wikipedia:User access levels|<abbr title="' + mw.config.get('wgUserGroups').join(', ') + '">usergroups</abbr>]])</sup> loaded [[' + mw.config.get('wgPageName').replace(/_/g,' ') + ']] at {'+'{#time: H:i:s, F j, Y|' + loadTS + '}} (UTC).\n';
taskBUG += 'The script found the following ' + externalLinks.length.toString() + ' link(s) and filtered out ' + (externalLinks.length - uniqueLinks.length).toString() + ' duplicate(s) and ' + (uniqueLinks.length - uniqueURLs.length).toString() + ' invalid link(s).  This left the ' + uniqueURLs.length + ' URL(s) listed below.';
var linkBUG = ''{'+'{Hst|reason=Links}}\n{'+'{Div col start|2}}\n# ' + externalLinks.join('\n# ') + '\n{'+'{Div col end}}\n{'+'{Hsb}}';
var urlBUG = '; URLs\n{'+'{Div col start|2}}\n# ' + uniqueURLs.join('\n# ') + '\n{'+'{Div col end}}';
if(externalLinks.length > 0){ var eL = true; } else { var el = false; } if(uniqueURLs.length > 0){ var eU = true; } else { var eU = false; } if(eL && eU){ var bugBUG = '{'+'{Hst|reason='; } if(eL){ bugBUG += 'Links'; } if(eL && eU){ bugBUG += ' and '; } if(eU){ bugBUG += 'URLs' } if(eL && eU){ bugBUG += ' returned by script}}\n'; } if(eL){ bugBUG += linkBUG; } if(eL && eU){ bugBUG += '\n'; } if(eU){ bugBUG += urlBUG; } if(eL && eU){ bugBUG += '\n{'+'{Hsb}}'; }
console.info('<!-- BUG REPORT HELPER CODE BELOW.  Do not include this comment. -->\n%s\n%s\n<!-- END OF BUG REPORT HELPER CODE. Do not include this comment. -->', taskBUG, bugBUG);

/* report TROFF */

var toolTip = 'Scan for copyright violations in the ' + uniqueURLs.length + ' unique URL';
if(uniqueURLs.length !== 1){ toolTip += 's'; }
toolTip += ' detected using Earwig\'s Copyvio Detector & Derrick Coetzee\'s Duplication Detector tools.  (';
if(uniqueURLs.length > 5){ toolTip += 'WARNING: '; }
toolTip += 'This will open ' + ((uniqueURLs.length*2)+1) + ' new tabs/windows.)';
var scanCV = mw.util.addPortletLink(
	'p-tb',
	'#',
	'CVDetector (' + ((uniqueURLs.length*2)+1) + ')',
	'p-cv-detector',
	toolTip
);

$( scanCV ).click( function ( e ) {
	e.preventDefault();
	if(uniqueURLs.length > 20){
		var lottaTabs = confirm('Are you SURE that you want to open ' + ((uniqueURLs.length*2)+1) + ' new tabs?');
		if(lottaTabs === true){
			window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')), '_blank');
			for(var i=0; i < uniqueURLs.length; i++){
				window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&url=' + encodeURIComponent(uniqueURLs[i]), '_blank');
				window.open('https://tools.wmflabs.org/dupdet/compare.php?url1=' + encodeURIComponent('https:' + wgServer + wgScriptPath + '/index.php?title=' + wgPageName) + '&url2=' + encodeURIComponent(uniqueURLs[i]), '_blank');
			}
		} else {
			alert('Only opening tabs for the first five links then');
			window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')), '_blank');
			for(var i=0; i < 5; i++){
				window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&url=' + encodeURIComponent(uniqueURLs[i]), '_blank');
				window.open('http:///tools.wmflabs.org/dupdet/compare.php?url1=' + encodeURIComponent('https:' + wgServer + wgScriptPath + '/index.php?title=' + wgPageName) + '&url2=' + encodeURIComponent(uniqueURLs[i]), '_blank');
			}
		}
	} else {
		window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')), '_blank');
		for(var i=0; i < uniqueURLs.length; i++){
			window.open('http://toolserver.org/~earwig/copyvios?lang=' + wgContentLanguage + '&project=' + wgSiteName.toLowerCase() + '&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&url=' + encodeURIComponent(uniqueURLs[i]), '_blank');
			window.open('http:///tools.wmflabs.org/dupdet/compare.php?url1=' + encodeURIComponent('https:' + wgServer + wgScriptPath + '/index.php?title=' + wgPageName) + '&url2=' + encodeURIComponent(uniqueURLs[i]), '_blank');
		}
	}
});