javascript - My Greasemonkey script is not finding the elements (links) on the page? - Stack Overflow

The website is: lexin.nada.kth.selexin#searchinfo=both,swe_gre,hej;My script is:function main(){var l

The website is: lexin.nada.kth.se/lexin/#searchinfo=both,swe_gre,hej;

My script is:

function main(){
  var links=document.getElementsByTagName("a");
  alert("There are " + links.length + "links.");
}

main();

Running the script gives me two alert messages saying

There are 0 links.

Any ideas why I can't get the right amount of links from the document? And why do I get the alert twice?

The website is: lexin.nada.kth.se/lexin/#searchinfo=both,swe_gre,hej;

My script is:

function main(){
  var links=document.getElementsByTagName("a");
  alert("There are " + links.length + "links.");
}

main();

Running the script gives me two alert messages saying

There are 0 links.

Any ideas why I can't get the right amount of links from the document? And why do I get the alert twice?

Share Improve this question edited Jan 13, 2016 at 3:48 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Mar 14, 2012 at 14:17 PithikosPithikos 20.4k17 gold badges123 silver badges146 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 8
  1. The alert fires more than once because that page contains iFrames (with, de facto, the same URL as the main page). Greasemonkey treats iFrames as if they were standalone web pages. Use @noframes to stop that.

  2. The script is not finding the links because they are added, by javascript, long after the page loads and the GM script fires. This is a mon problem with scripts and AJAX. A simple, robust solution is use waitForKeyElements() (and jQuery).

Here is a plete sample script that avoids the iFrames and shows how to get dynamic links:

// ==UserScript==
// @name     _Find elements added by AJAX
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @match    http://stackoverflow./questions/*
// @require  http://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github./raw/2625891/waitForKeyElements.js
// @noframes
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
var totalUsrLinks   = 0;

waitForKeyElements ("a[href*='/users/']", listLinks);

function listLinks (jNode) {
    var usrMtch     = jNode.attr ("href").match (/^.*\/users\/(\d+)\/.*$/);
    if (usrMtch  &&  usrMtch.length > 1) {
        totalUsrLinks++;
        var usrId   = usrMtch[1];
        console.log ("Found link for user: ", usrId, "Total links = ", totalUsrLinks);
    }
}

It's returning an HTMLcollection because of .getElementsByTagName and because of that, you will have to state the HTMLcollection with .getElementsByTagName and then find the length, and alert it. It will look like this...

 (function main(){
        var links = document.getElementsByTagName("a").length
        alert("There are "+ links + " links.");
        })()

I added an IIFE or an Immediately-Invoked-Function-Expression more on IIFEs here, so you don't have to call the function, and so the code is small and able to be "swallowed". lastly, it's alerting 2 alert boxes, because there's one[alert box] in the function and you're calling that function so it's going to do the same thing.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327558a4423053.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信