使用GM_xmlhttpRequest获取多个外部URL,将页面

添加到链接吗?

| 已解决,感谢Hellion的帮助! 这是代码:
// ==UserScript==
// @name          Facebook Comment Moderation Links
// @description   Appends story titles to Facebook Comment Moderation \"Visit Website\" links
// @include       http*://developers.facebook.com/tools/*
// ==/UserScript==

var allLinks, thisLink, expr, pageTitle, myURL, myPage, pageContent, title;

// grabbing URLs
function fetchPage(myPage, targetLink) {
        GM_xmlhttpRequest({
            method: \'GET\',
            url: myPage,
            onload: function(response){

                // get the HTML content of the page
                pageContent = response.responseText;

                // use regex to extract its h1 tag
                pageTitle = pageContent.match(/<h1.*?>(.*?)<\\/h1>/g)[0];

                // strip html tags from the result
                pageTitle = pageTitle.replace(/<.*?>/g, \'\');

                // append headline to Visit Website link
                title = document.createElement(\'div\');
                title.style.backgroundColor = \"yellow\";
                title.style.color = \"#000\";
                title.appendChild(document.createTextNode(pageTitle));
                targetLink.parentNode.insertBefore(title, targetLink.nextSibling);  

            }
        }); 
}


function processLinks() {

    // define which links to look for
    expr = \"//a[contains (string(), \'Visit Website\')]\";
    allLinks = document.evaluate(
        expr,
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);

    // loop through the links
    for (var i = 0; i < allLinks.snapshotLength; i++) {
        thisLink = allLinks.snapshotItem(i);    
        myURL = thisLink.getAttribute(\'href\');

        // follow Visit Website link and attach corresponding headline
        fetchPage(myURL, thisLink);
    }
}

// get the ball rolling
processLinks();
---更早的东西--- 我正在尝试制作一个Greasemonkey脚本,该脚本从一组链接的每一个中获取URL,并将页面的h1标签的内容附加到链接的末尾。 到目前为止,我可以获取它来显示URL本身,它不需要页面请求,但是不需要页面的h1标签内容。 我从该站点上的其他问题中了解到GM_xmlhttpRequest是异步的,并且我很确定这至少是原因的一部分。但是,我找不到针对此特定问题的解决方案。 下面是我到目前为止的代码。它用于Facebook的网站评论审核工具-在“主持人视图”中,每个评论都有一个链接“访问网站”,可将您带到评论所在的文章。 就像现在写的那样,它将在每个“访问网站”链接的后面附加HTTP状态代码,而不是页面标题,然后附加URL。状态代码部分只是一个占位符。我计划添加HTML解析等,以便稍后获取h1标签。 现在,我只是想让GM_xmlhttpRequest和内容插入匹配。 任何帮助解决此问题将不胜感激。谢谢!
var allLinks, thisLink, expr, pageTitle, myURL, pageContent, title;

// define which links to process
    expr = \"//a[contains (string(), \'Visit Website\')]\";
    allLinks = document.evaluate(
        expr,
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);

// cycle through links
for (var i = 0; i < allLinks.snapshotLength; i++) {

    thisLink = allLinks.snapshotItem(i);    
    myURL = thisLink.getAttribute(\'href\');

    GM_xmlhttpRequest({
        method: \'GET\',
        url: myURL,
        onload: function(responseDetails){

            pageTitle = responseDetails.status;

        }
    });

    // append info to end of each link 
    title = document.createElement(\'div\');
    title.style.backgroundColor = \"yellow\";
    title.style.color = \"#000\";
    title.appendChild(document.createTextNode(
        \' [\' + pageTitle + \' - \' + thisLink.getAttribute(\'href\') + \']\'));
    thisLink.parentNode.insertBefore(title, thisLink.nextSibling);  

}
    
已邀请:
        正如它所写的,是的,您遭受
GM_xmlhttpRequest()
调用的异步特性的困扰。循环将触发并开始获取所有pageTitle值,但将立即继续运行,而不等待请求完成,因此pageTitle(顺便说一下,您没有在任何地方声明)为null用于textNode。 要纠正这种情况,需要采取的第一步是将currently2ѭ调用之后当前的所有内容移至
onload: function()
定义的内部。然后,只有在检索到每个页面之后,您才能继续修改链接。 (我不确定也可能需要传递或重新获取
thislink
值的其他问题。)     
        您可以将以下3行更改为仅1行:
            // get the HTML content of the page
            pageContent = response.responseText;

            // use regex to extract its h1 tag
            pageTitle = pageContent.match(/<h1.*?>(.*?)<\\/h1>/g)[0];

            // strip html tags from the result
            pageTitle = pageTitle.replace(/<.*?>/g, \'\');
             pageTitle = $(\'h1\', response.response).text();
    

要回复问题请先登录注册