Участник:Js/checklinks.js
Перейти к навигации
Перейти к поиску
if (mw.config.get('wgNamespaceNumber')==0 && mw.config.get('wgAction')=='view'){
if (!window.$j) mw.loader.load('//bits.wikimedia.org/skins-1.5/common/jquery.min.js')
$(checkLinks)
}
function hasClass(node,className) {
return $(node).hasClass(className);
}
function checkLinks(){
var articlePath = mw.config.get('wgArticlePath').replace(/\$1/,'')
var dvContent = $('#bodyContent')
var dvOut = $('<div style="padding:5px; border:1px solid gray">')
.prependTo(dvContent.parent()).hide()
mw.util.addCSS('.highlighted {border-bottom:2px solid red !important}')
var htmLinks = [], iwRequest = {}, iwLine = {}
filterLinks()
//initally show extiw links
for (var i=0; i<htmLinks.length; i++)
if (hasClass(htmLinks[i].obj,'extiw')) Extiw(i)
//else if (hasClass(htmLinks[i].obj,'external')) checkExternal(i)
$(mw.util.addCSS('a.extiw {border-bottom:1px solid orange}')).attr('id','extiwcss')
$(mw.util.addPortletLink('p-cactions','#','[]','showlnk','Показать ссылки'))
.click(showAllLinks)
if (dvOut.children().length == 0) return
dvOut.prepend(closeLink(Extiw_close)).show()
$('<button>').text('Проверить по интервикам').bind('click',apiCheck).appendTo(dvOut)
window.clApiRecv = apiRecv
return
function Extiw(i){
var lnk = $(htmLinks[i].obj)
var line = $('<li style="list-style-type:none">')
//add link
var ma = lnk.attr('href').match(/^http:\/\/([a-z]+)\.wikipedia\.org\/wiki\/(.*)/)
if (!ma) return
var lang = ma[1], titl = canonical(decodeURIComponent(ma[2].replace(/_/g,' ')))
//for API request
iwRequest[lang] = (iwRequest[lang] || '') + '|'+titl
titl = lang+':'+titl
if (!iwLine[titl]) iwLine[titl] = []
iwLine[titl].push(line[0])
//create output line
line.append(lnk.clone(true),' ',
$('<span style="cursor:pointer; border-bottom:1px dotted gray" title="показать">[['+titl+']]</span>').bind('click',{link:lnk[0]},scrollToLink))
//prev links
var k = i-1
if (k>=0 && htmLinks[k].addr.match(/язык$/))
line.prepend($(htmLinks[k--].obj).clone(true),' ... ')
if (k>0 && hasClass(htmLinks[k].obj,'new'))
line.prepend($(htmLinks[k].obj).clone(true),' ... ')
dvOut.append(line)
}
function Extiw_close(){ $('#extiwcss').remove() }
function apiCheck(a){
$(this).remove()
for (var lang in iwRequest)
mw.loader.load('http://'+lang+'.wikipedia.org/w/api.php?'
+'action=query&format=json&callback=clApiRecv&requestid='+lang
+'&prop=langlinks&lllimit=500&titles='+iwRequest[lang].substring(1))
return false
}
function apiRecv(dat){
var lang = dat.requestid
var pages = dat.query.pages, pg, titl, iw, lines
var llcount = 0, found = 0
dvOut.append('<hr>')
for (var id in pages){
iw = -1
pg = pages[id]
if (!pg.langlinks) continue
llcount += pg.langlinks.length
for (var l=0; l<pg.langlinks.length; l++)
if (pg.langlinks[l].lang == mw.config.get('wgContentLanguage')) {iw = l; break }
if (iw >= 0){
found++
titl = lang + ':' + pg.title
local = pg.langlinks[iw]['*']
local = $('<a>').attr('href','/wiki/'+local).append('[\['+local+ ']]')
lines = iwLine[titl]
if (lines) for (var i=0; i<lines.length; i++)
$('<b>').append(' есть ', local.clone(true)).appendTo(lines[i])
dvOut.append('у '+ titl + ' есть ', local, '<br>')
}
}
dvOut.append(lang+': получено '+llcount+' интервик, найдено '+found+' локальных','<br>')
}
function filterLinks(){
aa = dvContent.find('a')
for (var i=0; i<aa.length; i++) filterLink(aa.eq(i))
function filterLink(lnk){
var url = lnk.attr('href'), par = lnk.parent()
if (
/^(javascript:|#cite_(note|ref))/i.test(url) ||
(url == '#' && lnk.attr('onclick')) ||
/jump-to-nav|contentSub/.test(par.attr('id')) ||
/\b(mw-magiclink-isbn|image)/.test(lnk.attr('class')) ||
/\b(editsection|toclevel-\d\d?|printfooter|usermessage|magnify)\b/.test(par.attr('class'))
) return
//check all parents
par = par[0]
while (par=par.parentNode)
if (/\b(catlinks|metadata|flaggedrevs)/.test(par.className)
|| 'contentSub' == par.id) return
//assing "type" and "addr" custom attribs to links
var url = decodeURL(lnk.attr('href'))
var tp = lnk.attr('class') && lnk.attr('class').split(' ')[0]
if (!tp && url.indexOf(articlePath)==0) tp = 'wiki' //normal link
switch (tp){
case 'external':
tp = 'ext'
if (url.indexOf(mw.config.get('wgServer'))==0){
tp = 'int'
url = url.replace(mw.config.get('wgServer'), '')
}
break
case 'mw-redirect':
tp = 'wiki' // and fall to the next
case 'stub': case 'wiki':
url = url.substring(articlePath.length)
break
case 'new':
url = url.match(/[&\?]title=([^&]+)/)[1]; break
break
}
//add to array
htmLinks.push({obj:lnk[0],type:tp,addr:url})
}
}
function decodeURL(url){
url = decode(url)
var ma = url.split('#')
if (ma.length > 1)
url = ma[0] + '#' + decode(ma[1].replace(/(\.)([0-9A-F][0-9A-F])/g, '%$2'))
return url
function decode(txt){
try { txt = decodeURIComponent(txt).replace(/_/g,' ') } catch(e) {}
return txt;
}
}
function canonical(tt) {return tt.charAt(0).toUpperCase() + tt.substring(1)}
function showAllLinks(){
lnkList = []
tbl = $('<table class=wikitable id=js-page-links border=1>\
<tr><th>type</th><th style="width:100px">link</th><th>text</th></tr></table>')
appendCSS('table#js-page-links td div {overflow-x:auto}\
td span.lnkname {padding:0 2px; margin:0 5px;\
border-bottom:1px dotted gray; cursor:pointer}\
span.lnkjump {cursor:pointer; padding-right:1em}')
var lnk, row, name, addr
for (var l in htmLinks){
lnk = $(htmLinks[l].obj)
addr = htmLinks[l].addr
row = lnkList[addr]
if (!row) //link not encountered yet, create a new row
lnkList[addr] = row = $('<tr>').append(
$('<td>').append(htmLinks[l].type.replace(/wiki|new/,'')),
$('<td>').append(lnk.clone(true).html(addr)),
'<td>') .appendTo(tbl)
name = lnk.html()
if (name == addr || canonical(name) == addr) name = ' '
row.children(':last')
.append($('<span class=lnkname>').append(name)
.bind('click',{link:lnk[0]},scrollToLink))
}
dvOut.empty().show().append(closeLink(), tbl)
ts_makeSortable(tbl[0])
}
function closeLink(func){
var x = $('<span style="border:1px solid gray;float:right;padding:0 2px;cursor:pointer;margin-right:1em">X</span>')
.bind('click', function(){if (this.func) func(); $(this).parent().empty().hide()})
if (func) x[0].func = func
return x
}
function scrollToLink(e){
var lnk = e.data.link, el = lnk
var scrollTop = $(window).scrollTop()
$(lnk).addClass('highlighted')
setTimeout(function(){unHighlight(lnk)}, 4000)
scrollMore()
function scrollMore(){
if (scrollTop != $(window).scrollTop()) return
el = el.parentNode
el.scrollIntoView()
setTimeout(scrollMore, 50)
}
function unHighlight(lnk){
$(lnk).removeClass('highlighted')}
}
}//main