MediaWiki:LinkPreview/code.js

/* */ /* smth like mw:Extension:Popups */ /* popup on link:hover */ /* maintainer: user:fngplg */ /* classes: main: npage-preview, image not found: npage-preview-noimage */ /* img:, text: */ (function wrapper ($) {   var Settings = window.pPreview || {};    Settings.debug = $.getUrlVar('debug') || (Settings.debug !== undefined ? Settings.debug : false);   //default values    var Defaults = {        defimage: 'https://vignette.wikia.nocookie.net/borderlands/images/0/05/Ajax.gif/revision/latest/scale-to-width-down/350?cb=20170626182120&path-prefix=ru',        noimage : 'https://vignette.wikia.nocookie.net/borderlands/images/f/f5/%D0%97%D0%B0%D0%B3%D0%BB%D1%83%D1%88%D0%BA%D0%B0.png/revision/latest/scale-to-width-down/200?cb=20160122074659&path-prefix=ru',    };//defaults    var pp = {};    pp.sync = []; //synchronization element    var ncache = []; //{href, data}    var loc = {lefts: 5, tops: 5}; //left: x, top: y, lefts: left-shift, clientx    var currentEl = {}; //{href, ?data}    //var api = new mw.Api;    var apiUri = new mw.Uri({path: '/api.php'});    var thisPage;// will be assigned later (init)    //exports    Settings.wrapper = wrapper;    Settings.context = this;    Settings.f = {init: init, main: main, createuri: createUri, getpreview: ngetPreview, showpreview: nshowPreview, hidepreview: nhidePreview, cache: ncache, ignoreimage: nignoreImage, ignorepage: nignorePage, ignorelink: nignoreLink, cacheof: ncacheOf, chkimagesrc: chkImageSrc, preprocess: preprocess, elvalidate: elValidate}; mw.loader.using(['mediawiki.util'], init);

function log { log.a = [].slice.call(arguments); log.a.unshift('pp'); if (Settings.debug) console.log.apply(this, log.a); }//log

pp.start = function (e) { //allows (true) processing for element e       if (e) { if (pp.sync.indexOf(e) > -1) { return false; }       }        Settings.process = true; pp.sync.push(e || Settings.process); return true; };//start pp.stop = function (e) { hlpaHover; pp.sync = pp.sync.slice(1); if (pp.sync.length === 0) { Settings.process = false; }   };//stop

pp.cachedupl = function { //check cache for href duplication var el = null; outer: for (var i = 0, len = ncache.length; i < len; i++) { for (var k = i + 1; k < len; k++) { if (ncache[i].href === ncache[k].href) { el = {v: ncache[i].href, i: i, k: k}; break outer; }           }//k inner loop }//i outer loop if (el) { console.log('pp.cachedupl found', el.v, el.i, el.k); }   };//cachedupl

function init { if (window.pPreview && window.pPreview.version) { log('init dbl run protection triggered'); return; }       Settings.version = '1.4'; log('init vrsn:', Settings.version); //show preview delay, ms       Settings.delay = Settings.delay !== undefined ? Settings.delay : 100; //suppress hover events for x ms       //Settings.throttling = timeout until x        Settings.throttle = Settings.throttle !== undefined ? Settings.throttle : 100; Settings.throttling = false; Settings.process = false;//processing data Settings.tlen = Settings.tlen !== undefined ? Settings.tlen : 1000; //max text length //do not remove portable infobox on preprocess stage Settings.pibox = Settings.pibox !== undefined ? Settings.pibox : false; //cache size Settings.csize = Settings.csize !== undefined ? Settings.csize : 100; Settings.defimage = Settings.defimage !== undefined ? Settings.defimage : Defaults.defimage; //default image path //no image found. class: npage-preview-noimage Settings.noimage = Settings.noimage !== undefined ? Settings.noimage : Defaults.noimage; //request to perform scaling Settings.scale = Settings.scale !== undefined ? Settings.scale : {r: '?', t: '/scale-to-width-down/350?'}; //container (#WikiaMainContent, #mw-content-text etc) Settings.dock = !!Settings.dock ? Settings.dock : '#mw-content-text'; Settings.RegExp = Settings.RegExp || {}; //regexps //images 2 ignore Settings.RegExp.iimages = Settings.RegExp.iimages || []; //pages 2 ignore Settings.RegExp.ipages = Settings.RegExp.ipages || []; //links 2 ignore Settings.RegExp.ilinks = Settings.RegExp.ilinks || []; //parents to ignore Settings.RegExp.iparents = Settings.RegExp.iparents || ['[id^=flytabs]']; //classes to ignore Settings.RegExp.iclasses = Settings.RegExp.iclasses || []; //Settings.RegExp.hash = Settings.RegExp.hash || new RegExp('#.*'); Settings.RegExp.wiki = Settings.RegExp.wiki || new RegExp('\/wiki\/', 'i'); //delete tags Settings.RegExp.dtag = Settings.RegExp.dtag || new RegExp('<.*>','gm'); //preprocess data (remove scripts) Settings.RegExp.prep = Settings.RegExp.prep || []; window.pPreview = Settings; thisPage = createUri(location).truepath; //should i ignore this page if (nignorePage(thisPage)) { mw.hook('wikipage.content').remove(main); log('ignore', thisPage); return; }       //run once importArticles({type: "style", articles: ["u:dev:MediaWiki:LinkPreview/code.css"]}); log('rmain'); if (Settings.debug) { Settings.cache = ncache; }       Settings.RegExp.ilinks.push(thisPage); // ignore this page Settings.RegExp.ilinks.push(new RegExp(apiUri.path)); //ignore unknown var r;       if (Settings.RegExp.prep instanceof RegExp) { r = Settings.RegExp.prep; Settings.RegExp.prep = [r]; }//if regexp.prep is regexp if (!(Settings.RegExp.prep instanceof Array)) { Settings.RegExp.prep = []; }//if regexp.prep is not array Settings.RegExp.prep.push(/ [\s\S]*?<\/script>/igm); Settings.RegExp.prep.push(/ [\s\S]*?<\/ref>/igm); Settings.defimage = chkImageSrc(Settings.defimage) ? Settings.defimage : Defaults.defimage; Settings.noimage = chkImageSrc(Settings.noimage) ? Settings.noimage : Defaults.noimage; Settings.f.pp = pp; //ajaxrc support window.ajaxCallAgain = window.ajaxCallAgain || []; window.ajaxCallAgain.push(main); mw.hook('wikipage.content').add(main); mw.hook('ppreview.ready').fire(Settings); //main; } //init function main ($cont) { //main var $content = $cont || $(Settings.dock); log('main.c:', $content); $content.find('a').each(function {           var $el = $(this);            if (elValidate($el)) { //internal link                //$el.hover(aHover, nhidePreview);                $el.off('mouseenter.pp mouseleave.pp');                $el.on('mouseenter.pp', aHover);                $el.on('mouseleave.pp', nhidePreview);            } // if internal link        }); //each a    } //main function elValidate ($el) { //returns false if element should be ignored var ahref = $el.attr('href'), bstop = false; //log('elValidate. el.h:', ahref); if (!ahref) return false; ahref = createUri(ahref); //log('elValidate.uri:', ahref); if (!ahref || (ahref.hostname !== apiUri.host) || nignoreLink(ahref.truepath)) { return false; }

//chk classes if ($.isArray(Settings.RegExp.iclasses)) { Settings.RegExp.iclasses.forEach(function(v) {               if ($el.hasClass(v)) {                    log('elValidate classes', v, ahref.truepath);                    //Settings.RegExp.ilinks.push(ahref.truepath);                    bstop = true;                }            }); }       //log('elValidate classes', bstop); if (bstop) return false;

//chk parents if ($.isArray(Settings.RegExp.iparents)) { Settings.RegExp.iparents.forEach(function(v) {               if ($el.parents(v).length) {                    log('elValidate parents', v, ahref.truepath);                    //Settings.RegExp.ilinks.push(ahref.truepath);                    bstop = true;                }            }); }       //log('elValidate parents', bstop); if (bstop) return false; return true; }//elValidate function chkImageSrc (src) { //is src belongs to wikia if (!src) return false; var url; try { url = new mw.Uri(src); return (/(\.wikia\.com|\.wikia\.nocookie\.net)$/.test(url.host)); }       catch (e) { return false; }       return false; }//chkimagesrc function preprocess (text) { //prep must be non-empty array (script removing at least, added in the init) if (!(Settings.RegExp.prep instanceof Array) || Settings.RegExp.prep.length < 1) return ''; var s = text; Settings.RegExp.prep.forEach(function (v) {           s = s.replace(v, '');        }); return s;   }//preprocess function createUri (href, base) { var h;       try { h = new mw.Uri(href.toString); h.pathname = h.path; h.hostname = h.host; } catch (e) { h = undefined; log('createUrl.e:', e); }       if (h) { h.truepath = decodeURIComponent(h.pathname.replace(Settings.RegExp.wiki, '')); }       return h;    } //createUri function escapeRegExp(str) { return str.replace(/([.*+?^=!:${}|\[\]\/\\])/g, "\\$1"); } //escapeRegExp function hlpaHover { //aHover helper if (Settings.throttling) { clearTimeout(Settings.throttling); Settings.throttling = false; }   }//hlpaHover function aHover (ev) { //a hover handler ev.stopPropagation; log('ahover ', Settings.throttling, currentEl.href); //suppress some events if (Settings.throttling || Settings.process) { return false; }       Settings.throttling = setTimeout(hlpaHover, Settings.throttle); var hel = createUri($(ev.currentTarget).attr('href')).truepath; //if link already in process if (currentEl.href == hel) { return false; }       currentEl.href = hel; //if link determined be ignored if (nignoreLink(currentEl.href)) { return true; } //if ignore link //set coords loc.left = ev.pageX; loc.top = ev.pageY; loc.clientX = ev.clientX; loc.clientY = ev.clientY; log('ahover ev:', ev, 'cel:', currentEl); setTimeout(ngetPreview.bind(this, ev), Settings.delay); return false; } //ahover function getObj (data, key) { //traverse through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { if (k === key) { ret.push(data[k]); }               r=getObj(data[k], key); if (r) ret=ret.concat(r); } //if obj } //for k in data return ret; } //getObj function getVal (data, key) { //travers through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { r=getVal(data[k], key); if (r) { ret=ret.concat(r); }           } else { if (k === key) { ret.push(data[k]); }           } //if obj } //for k in data return ret; } //getVal function hlpPreview (href, div, img, force) { //preview helper //load img and add to div var im, d;       im = $('img', div); if (img) { //let vignette do scale im.attr('src', Settings.scale ? img.replace(Settings.scale.r, Settings.scale.t) : img); } else { im.attr('src', Settings.noimage); im.addClass('npage-preview-noimage'); } //if img d = {href: href, data: div}; ncache.push(d); if (Settings.debug) window.pPreview.pdiv = d.data; nshowPreview(d.data, d.href, force); pp.stop; } //hlpPreview function ngetPreview (ev, forcepath) { var nhref = forcepath || createUri($(ev.currentTarget).attr('href')).truepath; if (!pp.start(nhref)) { //this href already started to process log('gp suppressed dbl processing for', nhref); return; }       //save bandwith log('gp href: ', nhref, ' curel.href: ', currentEl.href, nhref === currentEl.href); if (!forcepath && (nhref != currentEl.href)) { pp.stop; return; }       var ndata = ncacheOf(nhref); log('gp x:', loc.left, 'y:', loc.top); if (ndata) { log('gp show preview', ndata); nshowPreview(ndata.data, nhref, forcepath ? true : false); pp.stop; return false; } //if data //get data var apipage = apiUri.clone.extend({action: 'parse', page: nhref, section: 0,                   prop: 'images|text', format: 'json', disablepp: , redirects: }); log('gp apip: ', apipage.toString); $.getJSON(apipage).done(function(data) {           //parse: {text: {*: text}, images: []}            if (!data.parse) {                log('gp apip. no valid data in', data);               Settings.RegExp.ilinks.push(nhref); //and ignore it                pp.stop;                return this;            }            var img = data.parse.images.map(function(value, index) { if (nignoreImage(value)) { return false; } else { return value; }           }).filter(Boolean)[0];            //img = $(img);            var text = data.parse.text['*'];            log('gp apip img:', img, 'text:', {text: text});            if (!img && !text) {                Settings.RegExp.ilinks.push(nhref); //and ignore it                pp.stop;                return this;            }            //preprocess (cleanup)            text = preprocess(text);            text = $(' ', {class: 'tmpdivclass', style: 'visibility:hidden;display:none;'}).html(text);            if (!Settings.pibox) { //remove portable infobox                text.find('aside').prevAll.remove;                text.find('aside').remove;            }            //convert 2 text            text = text.text;            //text clean up            text = text ? text.replace(Settings.RegExp.dtag, ) : ;            text = text.trim.substr(0, Settings.tlen); if (Settings.debug) { Settings.pptext = text; Settings.ppdata = data; log('gp img: ', img, ' text: ', {text: text}); }           var div = $(' ', {class: 'npage-preview'}); var d = {}, twrap, iwrap; if (text.length > 0) { twrap = $(' ', {text: text}); //wrap.text(text); div.append(twrap); } //if text iwrap = $(' ', {src: Settings.defimage}); div.prepend(iwrap); if (img) { //action=query&titles=file:.jpg&iiprop=url&prop=imageinfo&format=xml var im = 'file:' + img.trim; var apiimage = apiUri.clone.extend({action: 'query', redirects: '',                           titles: im, iiprop: 'url', prop: 'imageinfo', format: 'json'}); log('gp apii: ', apiimage.toString); $.getJSON(apiimage.toString).done(function(data) {                   log('gp apii done:', data);                    var im, d1;                    d1 = data.query;                    if (d1.redirects) {                        var imRed = getVal(getObj(d1, 'redirects'), 'to');                        log('gp img redir to', imRed);                        if (imRed.length > 0) {                            imRed = imRed[0];                        } else {                            //no url found                            iwrap.attr('src', Settings.noimage);                            log('gp img redir.to not found in', d1);                            return this;                        }                        var apiim = apiimage.clone.extend({titles: imRed});                        //resolve redirect                        log('gp resolv redir:', apiim.toString);                        $.getJSON(apiim.toString, function(data) { var im = getVal(getObj(data, 'pages'), 'url'); if (im.length > 0) { im = im[0]; } else { //no url found. again im = false; }                           hlpPreview(nhref, div, im, forcepath ? true : false); }); //getjson. resolve redirect                   } else {                        im = getVal(getObj(d1, 'imageinfo'), 'url');                        if (im.length > 0) {                            im = im[0];                        } else {                            im = false;                        }                        hlpPreview(nhref, div, im, forcepath ? true : false);                   } //if redirects                    return this; //should be promise. but well                }).fail(function(obj, stat, err) {                    log('gp img api fail', obj, stat, err);                    hlpPreview(nhref, div, false, forcepath ? true : false);                   return this;                });//img fail } else { //no img hlpPreview(nhref, div, false, forcepath ? true : false); }//if img })//get page data.done       .fail(function(obj, stat, err){ log('pg get page data fail', obj, stat, err); pp.stop; });//get page data.fail       //pp.stop;        return false;    } //getpreview    function nshowPreview (data, target, force) {        log('sp', data, target, force);        if (!force && (currentEl.href !== target)) {            return false; //other hover processing yet        }        log('sp data:', data);        //nhidePreview;        $('.npage-preview').remove; //remove artefacts        $('body').append($(data));

//prehide data $(data).css({left: -10000, top: -10000}); $(data).show(200, function { //;//fadeIn('fast');           //reposition works well with pre-set fixed data bounds            if ((loc.clientY + $(data).height) > $(window).height) {                loc.top -= ($(data).height + loc.tops);            } else {                loc.top += loc.tops;            }//if top>window            if ((loc.clientX + $(data).width) > $(window).width) {                loc.left -= ($(data).width + loc.lefts);            } else {                loc.left += loc.lefts;            }//if left>window            //move preview to target location            log('sp loc', loc);            loc.left = loc.left > 0 ? loc.left : 0;            loc.top = loc.top > 0 ? loc.top : 0;            $(data).css({ left: force ? $('body').scrollLeft : loc.left, top: force ? $('body').scrollTop : loc.top});           mw.hook('ppreview.show').fire(data);        });//data.show.done } //showpreview function nhidePreview (data) { currentEl.href = ''; $('.npage-preview').remove; //clear throttling hlpaHover; } //hidepreview function nignoreImage (name) { //true if image should be ignore //name = name.replace(/(file):/im, ''); //name = name.charAt(0).toUpperCase + name.slice(1); for (var i = 0, len = Settings.RegExp.iimages.length; i < len; i++) { if (Settings.RegExp.iimages[i] instanceof RegExp) { if (Settings.RegExp.iimages[i].test(name)) return true; } else { if (name === Settings.RegExp.iimages[i]) return true; } //if regexp }       return false; } //nignoreimage function nignorePage (name) { //true if page should be ignore var a = Settings.RegExp.ipages; for (var i = 0, len = a.length; i < len; i++) { if (a[i] instanceof RegExp) { if (a[i].test(name)) return true; } else { if (name === a[i]) return true; } //if regexp }       return false; } //nignorepage function nignoreLink (name) { //true if link should be ignore var a = Settings.RegExp.ilinks; for (var i = 0, len = a.length; i < len; i++) { if (a[i] instanceof RegExp) { if (a[i].test(name)) return true; } else { if (name === a[i]) return true; } //if regexp }       return false; } //nignorelink function ncacheOf (href) { //returns cached obj or null if (ncache.length > Settings.csize) ncache = []; //clear cache for (var i = 0, len = ncache.length; i < len; i++) { if (ncache[i].href === href) { log('cache found:', href, 'data:', ncache[i].data); //window.ppcdata = ncache[i]; return ncache[i]; }       }        return null; } //ncacheof })(jQuery);