User:Alexis Jazz/MVattrib.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Alexis Jazz/MVattrib. |
//<nowiki>
//[[User:Alexis Jazz/MVattrib]], addon that makes MediaViewer (https://www.mediawiki.org/wiki/Reading/Multimedia/Media_Viewer) license compliant in certain cases where it otherwise isn't (https://phabricator.wikimedia.org/T68606)
if ( mw.config.get('wgMediaViewerOnClick') ) {
MVattrib = {};
MVattrib.msgs = {
'author': 'Author',
'license': 'License',
'scroll': ' Scroll down for more complete information. '
};
MVattrib.debugmode = true;
MVattrib.debug = function(text) {
if ( MVattrib.debugmode == true ) {
if ( typeof text == 'string' ) {
console.log('MVattrib: ' + text);
} else {
console.log('MVattrib:');
console.log(text);
}
}
};
MVattrib.debug('MVattrib loaded');
MVattrib.getWikitextFromExport = function(text) {
if ( text.match(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/) ) {
return text.replace(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/, '$1').replace(/\<\;/g, '<').replace(/\>\;/g, '>').replace(/\&\;/g, '&');
} else {
return '';
}
};
detectKey = function(pressedkey) {
if ( ( pressedkey.key == 'ArrowLeft' || pressedkey.key == 'ArrowRight' ) && $('.mw-mmv-prev-image')[0] && $('.mw-mmv-next-image')[0] ) {
MVattrib.addAttribution();
}
};
document.addEventListener('keydown', detectKey);
MVattrib.addAttribution = function() {
MVattrib.debug('run MVattrib.addAttribution');
var waitToAddAttrib = setInterval(function () {
clearInterval(waitToAddAttrib);
if ( $('.mw-mmv-prev-image')[0] && $('.mw-mmv-next-image')[0] ) {
$('.mw-mmv-prev-image')[0].addEventListener('click', MVattrib.addAttribution);
$('.mw-mmv-next-image')[0].addEventListener('click', MVattrib.addAttribution);
}
if ( $('.mw-mmv-image-metadata')[0] ) {
MVattrib.title = decodeURIComponent(document.location.hash.replace(/\#\/media\//, '').replace('_', ' '));
var apiCommons = new mw.ForeignApi('https://commons.wikimedia.org/w/api.php');
apiCommons.get( {
format: 'json', action: 'query', export: 'true', titles: MVattrib.title,
} ).done( function ( data ) {
MVattrib.debug(data);
MVattrib.wikiText = MVattrib.getWikitextFromExport(data.query.export["*"]);
MVattrib.licenseSection = MVattrib.wikiText.match(/\=\=.*\{\{int\:license\-header\}\}.*\=\=[^]*/);
MVattrib.permissionParam = MVattrib.wikiText.match(/\|\s*[Pp]ermission\s*\=\s*(\{\{[^\{\}]*\}\}|\[\[[^\[\]]*\]\]|[^\|])*/);
//conditions to append attribution, detect if MV is likely to have screwed up and if we're likely to be able to do better
if ( (MVattrib.licenseSection || MVattrib.permissionParam) && ( MVattrib.wikiText.match(/\|\s*[Aa]uthor\s*\=\s*\{\{[Cc]reator\:[^\}\n]*\}\}[ \n]*[^ \n]/) || MVattrib.wikiText.match(/\{\{fop/i) ) ) {
MVattrib.debug('okay let\'s add to the attribution and license info');
MVattrib.attribution = MVattrib.wikiText.match(/\|\s*[Aa]uthor\s*\=\s*(\{\{[^\{\}]*\}\}|\[\[[^\[\]]*\]\]|[^\|])*/)[0].replace(/\|\s*[Aa]uthor\s*\=\s*/, '').replace(/\s*$/, '').replace(/\n\n/g, '\n').replace(/\{\{\!\}\}/g, '|');
if ( MVattrib.licenseSection && MVattrib.licenseSection[0] ) {
MVattrib.debug('extract license section from wikitext');
MVattrib.licenseSectionWikitext = MVattrib.licenseSection[0].replace(/\=\=.*\{\{int\:license\-header\}\}.*\=\=\s*/, '').replace(/\n\s*\=\=[^]*/, '').replace(/\s*$/, '').replace(/\n\n/g, '\n');
} else {
MVattrib.licenseSectionWikitext = '';
}
if ( MVattrib.permissionParam && MVattrib.permissionParam[0] ) {
MVattrib.debug('extract permission section from wikitext');
MVattrib.licensePermissionWikitext = MVattrib.permissionParam[0].replace(/\|\s*[Pp]ermission\s*\=\s*/, '').replace(/\s*$/, '').replace(/\n\n/g, '\n');
} else {
MVattrib.licensePermissionWikitext;
}
if ( MVattrib.licensePermissionWikitext ) {
MVattrib.licensePermissionWikitext = MVattrib.licensePermissionWikitext + '<br/>';
}
MVattrib.license = MVattrib.licensePermissionWikitext + MVattrib.licenseSectionWikitext;
MVattrib.tableClass = '';
if ( MVattrib.attribution ) {
MVattrib.authorCol = MVattrib.msgs.author;
} else {
MVattrib.authorCol = '';
}
if ( MVattrib.license ) {
MVattrib.licenseCol = MVattrib.msgs.license;
} else {
MVattrib.licenseCol = '';
}
MVattrib.compliantAttrib = '{| class="' + MVattrib.tableClass + '" style="width:98%;margin-left:1%;margin-right:1%" \n! style="text-align:left"|\'\'\'' + MVattrib.authorCol + '\'\'\'\n! style="width:2em" |\n! style="text-align:left"|\'\'\'' + MVattrib.licenseCol + '\'\'\'\n|-\n| style="vertical-align:top" |' + MVattrib.attribution + '\n|\n| style="vertical-align:top" |' + MVattrib.license + '\n|-\n| colspan="3" |[[w:en:User:Alexis Jazz/MVattrib|About this interface add-on]]\n|}';
MVattrib.scrollNotice = document.createElement('span');
MVattrib.scrollNotice.id = 'MVattribScrollNotice';
MVattrib.scrollNotice.innerHTML = MVattrib.msgs.scroll;
apiCommons.get( {
format: 'json', action: 'parse', title: MVattrib.title, pst: '1', prop: 'text', formatversion: '2', text: MVattrib.compliantAttrib
} ).done( function ( data ) {
MVattrib.debug(data);
if ( $('#MVattrib')[0] ) {
MVattrib.debug('updating license info');
$('#MVattrib')[0].innerHTML = data.parse.text;
} else {
MVattrib.debug('adding license info');
$('.mw-mmv-image-metadata').append('<div id="MVattrib">' + data.parse.text + '</div>');
$('.mw-mmv-credit')[0].prepend(MVattrib.scrollNotice);
}
});
} else if ( $('#MVattrib')[0] && $('#MVattribScrollNotice')[0] ) {
MVattrib.debug('new image loaded, removing author+license of previous image');
$('#MVattrib')[0].outerHTML = '';
$('#MVattribScrollNotice')[0].outerHTML = '';
} else {
MVattrib.debug('nothing to do');
}
});
}
}, 1000);
};
if ( document.location.hash.match(/\#\/media\//) ) {
MVattrib.addAttribution();
}
var waitToAddAttrib = setInterval(function () {
clearInterval(waitToAddAttrib);
for (int = 0; int < document.getElementsByClassName('image').length; int++) {
document.getElementsByClassName('image')[int].addEventListener('click', MVattrib.addAttribution);
}
}, 1000);
}
//</nowiki>