User:Jitrixis/itemNotice.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/**********************************************************************************
***********************************************************************************
** _ _ _ _ _ _ **
** (_) | | \ | | | | (_) **
** _| |_ ___ _ __ ___ | \| | ___ | |_ _ ___ ___ **
** | | __/ _ \ '_ ` _ \| . ` |/ _ \| __| |/ __/ _ \ **
** | | || __/ | | | | | |\ | (_) | |_| | (_| __/ **
** |_|\__\___|_| |_| |_|_| \_|\___/ \__|_|\___\___| v1.0 **
** **
** ----------------------------------------------------------------------------- **
** How To use: **
** Add to your common.js [[Special:MyPage/common.js]], **
** importScript( 'User:Jitrixis/itemNotice.js' ); **
** **
** Links: **
** [[User:Jitrixis/itemNotice.js]] **
** [[User talk:Jitrixis/itemNotice.js]] **
** **
** Read me: **
** **
***********************************************************************************
**********************************************************************************/
( function ( mw, $ ) {
'use strict';
var itemId, itemLink, itemTitle, isProperty = false, isQuery = false;
/**
* Return item Number.
*/
itemId = mw.config.get( 'wbEntityId' );
if( !itemId ) {
return;
}
itemLink = itemId.toUpperCase();
if(itemLink.substr(0,1) === "P"){
itemLink = "Property:" + itemLink;
isProperty = true;
}
if(itemLink.substr(0,1) === "Q"){
isQuery = true;
}
/**
* Return the title of the item
*/
itemTitle = $(document).attr('title');
itemTitle = itemTitle.replace(/(.*) \- Wikidata/g , "$1");
/**
* Display information on an item or a property
*/
function itemNotice(){
$.ajax({
url: mw.util.wikiScript( 'api' ),
//async: false,
type: 'GET',
dataType: 'json',
data: {
action: 'wbgetentities',
format: 'json',
ids: itemId,
props: "aliases",
languages: "en"
},
success: function ( data ) {
if(data.entities[itemId].aliases.hasOwnProperty("en")){
for(var nb in data.entities[itemId].aliases.en){
var alias = data.entities[itemId].aliases.en[nb].value;
var regEX = /^#[a-zA-Z]{4,6}:.*$/g;
if(regEX.test(alias)){
var display = alias.replace(/^#([a-zA-Z]{4,6}):.*$/g, "$1");
var template = alias.replace(/^#[a-zA-Z]{4,6}:(.*)$/g, "$1");
template = template.replace(/!/g, "|");
if(display.toUpperCase() === "HEAD"){
topNotice(template);
}else if(display.toUpperCase() === "FOOT"){
bottomNotice(template);
}else if(display.toUpperCase() === "ASIDE"){
asideNotice(template);
}else if(display.toUpperCase() === "NOTICE"){
autoNotice(template);
}
}
}
}
}
});
}
/**
* Translate Wikitexte to Html
*/
function wiki2html ( template, location ){
var wikitexte = "{{" + template + "|title=" + itemTitle + "|item=" + itemLink + "|id=" + itemId + "|isProperty=" + isProperty + "|isQuery=" + isQuery + "|location=" + location + "}}";
var htmltext;
$.ajax({
url: mw.util.wikiScript( 'api' ),
async: false,
type: 'GET',
dataType: 'json',
data: {
action: 'parse',
format: 'json',
title: 'Wikidata:Main_Page',
text: wikitexte
},
success: function ( data ) {
if(data.parse.text.hasOwnProperty("*")){
htmltext = data.parse.text["*"];
}
}
});
return htmltext;
}
/**
* Display a Notice at the top of the Item
*/
function topNotice( notice ){
notice = wiki2html(notice, "HEAD");
notice = '<div class="topNotice" style="clear:both">' + notice + '</div>';
$( notice ).prependTo( '#mw-content-text' );
}
/**
* Display a Notice at the bottom of the Item
*/
function bottomNotice( notice ){
notice = wiki2html(notice, "FOOT");
notice = '<div class="bottomNotice" style="clear:both"><br/>' + notice + '</div>';
$( notice ).appendTo( '#mw-content-text' );
}
/**
* Display a Notice at the bottom of the Item
*/
function asideNotice( notice ){
notice = wiki2html(notice, "ASIDE");
notice = '<div class="asideNotice" style="width: 97%; margin-left: 30px;clear:both">' + notice + '</div>';
$( notice ).appendTo( '#wb-widget-container-' + itemId );
}
/**
* TODO : AUTO DETECT
*/
function autoNotice (notice){
notice = wiki2html(notice, "AUTO");
if(/itemNotice:[HEAD|Head|head]/gmi.test(notice)){
notice = '<div class="topNotice" style="clear:both">' + notice + '</div>';
$( notice ).prependTo( '#mw-content-text' );
}else if(/itemNotice:[FOOT|Foot|foot]/gmi.test(notice)){
notice = '<div class="bottomNotice" style="clear:both"><br/>' + notice + '</div>';
$( notice ).appendTo( '#mw-content-text' );
}else if(/itemNotice:[ASIDE|Aside|aside]/gmi.test(notice)){
notice = '<div class="asideNotice" style="width: 97%; margin-left: 30px;clear:both">' + notice + '</div>';
$( notice ).appendTo( '#wb-widget-container-' + itemId );
}else{
asideNotice(notice);
}
}
$( document ).ready( itemNotice );
} ( mediaWiki, jQuery ) );