将文本框附加到维基百科缩略图

Appending textbox to Wikipedia thumbnail picture

本文关键字:百科 略图 文本      更新时间:2023-09-26

编写一个小的Javascript代码,将点击的href中的文本添加到维基百科页面的右上角。例如,如果我单击Sly和Robbie中的Jamaican链接,就会在右上角的页面上添加一个文本框。

注意:要测试此代码,请将其复制并粘贴到控制台,然后单击一些链接。

// Detect click on href
$("a").click(function(event){
    // Capture the href from the selected link
    var link = this.href;
    $.ajax({
        type: 'get',
        url: link
    }).done(function(data){    
        // Find only the body text (not the titles.. or unnecessary text tips)
        var bodyText = $(data).find('#bodyContent #mw-content-text p').text();
        // Get the length of the text
        var length = bodyText.length;
        // Replace the text only if there is text in the clicked link
        if(length > 0) {
            replaceThumbnail(bodyText);
        } else {
            alert("No text found!");
        }
    });
    // Prevent the link from being executed
    return false;
});
/**
 * Append clicked body text to page thumbnail
 * @param  {string} text Body text
 */
function replaceThumbnail(text) {
    $(".infobox .vcard .plainlist").addClass($(".infobox").html(text));
}

我现在特别关注的是replaceThumbnail函数。我正试图将(点击链接的)正文添加到维基百科页面缩略图的右上角。然而,在这个代码中,我只是用这个文本替换缩略图。

该方法的问题是您的选择器

$(".infobox")

选择整个框并将其内容替换为文本。

.html(text)

我已经修改了下面的替换缩略图功能:

function replaceThumbnail(text) {
    $(".infobox .vcard .plainlist").addClass($(".infobox tbody").prepend(text));
}

这将把你的文本放在信息框的最上面。