如何在我的页面中显示其他页面的图片

问题描述 投票:0回答:2

我想在现有页面上启动一个greasemonkey插件。该插件应该自动获取并显示一些图像,每个图像来自不同的页面。

我想过使用

jQuery.get("link", function(data))
并隐藏页面并仅显示图像,但平均显示 4 个图像我应该将 6 个网页加载到当前网页中,这会造成加载延迟。

是否有任何其他解决方法来创建一个函数,在后台或另一个选项卡中加载所有图像页面的页面 html,并获取该页面中

href
标签的
<a>
到我的页面并加载我的页面中只有图像吗?

javascript jquery
2个回答
0
投票

您可以尝试下面的解决方案。 只需将您想要的 URL 放入“pages”数组中即可。当脚本运行时,它会在后台进行 Ajax 调用。当它们准备好时,它会搜索返回的图像源并随机选择一张。如果找到,它会将图像包装在指向找到它的页面的链接中(或者如果可用,则将图像的 url )并将链接的图像插入到您自己的当前页面正文的顶部。

您可以通过将代码粘贴到浏览器的 JavaScript 控制台来尝试代码,它会将图像添加到当前页面。

您还可以在这里看到演示:http://jsfiddle.net/3Lcj3918/3/

//pages you want
var pages =
[
    'https://en.wikipedia.org/wiki/Special:Random',
    'https://en.wikipedia.org/wiki/Special:Random',
    'https://en.wikipedia.org/wiki/Special:Random',
    'https://en.wikipedia.org/wiki/Special:Random',
    'https://en.wikipedia.org/wiki/Special:Random'
]

//a simple function used to make an ajax call and run a callback with the target page source as an argument when successful
function getSubPageSource(url, successCallback)
{
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
            //when source returned, run callback with the response text
            successCallback(xhr.responseText);
        }
    };

    //requires a proxy url for CORS
    var proxyURL = 'https://cors-anywhere.herokuapp.com/';

    xhr.open('GET', proxyURL+url, true);

    //set headers required by proxy
    xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
    xhr.setRequestHeader("Access-Control-Allow-Origin","https://cors-anywhere.herokuapp.com/");
    xhr.send();
}

//a function that extract images from given url and inserts into current page
function injectImagesFrom(url)
{
    getSubPageSource(url, function(data)
    {
        //trim source code to body only
        var bodySource = data.substr(data.indexOf('<body ')); //find body tag
        bodySource = bodySource.substr(bodySource.indexOf('>') + 1); //finish removing body open tag
        bodySource = bodySource.substring(0, bodySource.indexOf('</body')); //remove body close tag

        //create an element to insert external source
        var workingNode = document.createElement("span");
        //insert source
        workingNode.innerHTML = bodySource;

        //find all images
        var allImages = workingNode.getElementsByTagName('img');

        //any images?
        if (allImages.length > 0)
        {
            //grab random image
            var randomIndex = Math.floor(Math.random() * allImages.length);
            var randomImage = allImages.item(randomIndex);
            //add border
            randomImage.setAttribute('style', 'border: 1px solid red;');
            //restrain size
            randomImage.setAttribute('width', 200);
            randomImage.setAttribute('height', 200);

            //check if parent node is a link
            var parentNode = randomImage.parentNode;
            if (parentNode.tagName == 'A')
            {
                //yes, use it
                var imageURL = parentNode.getAttribute('href');
            }
            else
            {
                //no, use image's page's url
                var imageURL = url;
            }

            //add a link pointing to where image was taken from
            var aLink = document.createElement("a");
            aLink.setAttribute('href', imageURL);
            aLink.setAttribute('target', '_blank');

            //insert image into link
            aLink.appendChild(randomImage);

            /* INSERT INTO PAGE */

            //insert image in beginning of body
            document.body.insertBefore(aLink,document.body.childNodes[0]);

            //remove working node children
            while (workingNode.firstChild) {
                workingNode.removeChild(workingNode.firstChild);
            }
            //unreference
            workingNode = null;
        }
    });
}

for (var ii = 0, nn = pages.length; ii < nn; ii++)
{
    injectImagesFrom(pages[ii]);
}

0
投票

[在此输入图像描述][1]

[1]:https://i.sstatic。净/pzc1I4Mf。 .jpg

© www.soinside.com 2019 - 2024. All rights reserved.