MediaWiki:Gadget-GoogleImagesTineye.js

Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.

// Forked from [[:c:MediaWiki:Gadget-GoogleImagesTineye.js]].

$(function () {
    'use strict';
    if (mw.config.get('wgDBname') != 'viwiki' || mw.config.get('wgNamespaceNumber') !== 6 || mw.config.get('wgAction') !== "view" || !document.getElementById('file'))
        return;

    var img = document.getElementById('file').getElementsByTagName('img');
    if (!img || !img.length)
        return; // No preview image, e.g. for large PNGs

    img = img[0];
    var imgURL = img.parentNode.href;
    if (!imgURL)
        return; // This occurs with thumbs of videos/sounds for instance

    if (img.width <= 300)
        imgURL = img.src; // Image smaller than 300px width
    else { // Get thumb url
        var n = imgURL.indexOf("/vi/") + 4;
        imgURL = imgURL.substr(0, n) + "thumb/" + imgURL.substr(n);
        n = imgURL.lastIndexOf('/') + 1;
        imgURL += "/300px-" + imgURL.substring(n);
        if (!imgURL.slice(-3).indexOf("svg"))
            imgURL += ".png";
    }

    var portletLink = mw.util.addPortletLink('p-cactions', 'https://www.google.com/searchbyimage?image_url=' + encodeURIComponent(imgURL), 'Google Images', 'ca-googleimages', null);
    if (portletLink && portletLink.children[0]) portletLink.children[0].target = '_blank';

    portletLink = mw.util.addPortletLink('p-cactions', 'https://tineye.com/search?url=' + encodeURIComponent(imgURL) + '&sort=size&order=desc', 'TinEye', 'ca-tineye', null);
    if (portletLink && portletLink.children[0]) portletLink.children[0].target = '_blank';
});