0

I'm trying to write some comment expander (as greasemonkey script) for livejournal, and get stuck with click simulating on the expand link:

<div style='margin-top: 3px; font-size: smaller'>
 (<a href="http://unique page url" rel="nofollow">Reply</a>) 
 (<a href='page url'>Parent</a>) 
 (<a href='http://unique page url'>Thread</a>) 
 <span id='expand_*'>(<a href='http://unique page url' onclick="ExpanderEx.make(event,this,'http://unique page url','*',true)">Expand</a>)</span>
    <div class="quickreply" id="ljqrt*" style="display: none;"></div>
</div>

I tried this code to activate all links on the page, but failed:

$(document).ready(function() {
  $('a').each(function() {
   $(this).[0].click();
  });
});

Any help would be much appreciated, because I'm totally newbie with jquery.

UPD. I modify script, so it looks like this:

jQuery(document).ready(function() { 
 console.log("function execute"); 
 $('[id^="expand_"] a').click(); 
 console.log("function Log");
 console.log($()); 
}); 

Console output in firebug after execution is:

function execute  --- lj_expa...user.js (line 10) 
TypeError: $(...) is null --- lj_expa...user.js (line 11) 
Server did not return the new auth_token, further request may fail Error: Permission denied to access property 'toString' --- ??.ljli...3140103 (line 1372)

UPD. Adding the line below solved my problem, both selectors work correct!

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

Anubelora
  • 3
  • 2
  • What do you mean by "activate all links"? – Roamer-1888 Oct 11 '14 at 22:17
  • @Roamer-1888 Simply click on links, that are selected by $('a') – Anubelora Oct 12 '14 at 05:23
  • 1
    Why would you want to click on all links. Surely you want to click on just one. – Roamer-1888 Oct 13 '14 at 11:16
  • @Roamer-1888 I try to explain. Livejournal is a blogging platform, that uses conversation threading for commentary. If the count of replies to entry reaches (don't remember exact value) 50, then I can't see all replies, only a top of answer hierarchy, until use expand script. Because I often read such posts, an idea comes over my mind - to write a script, that activates expand links, after I load a page. – Anubelora Oct 13 '14 at 11:57
  • So this is a Greasemonkey script? – Roamer-1888 Oct 13 '14 at 14:31
  • Yes, my fault, I completely forgot to mention this. – Anubelora Oct 13 '14 at 14:53
  • 1
    It's slightly confusing in that the HTML in the question appears to be some sort of template. I'm guessing that `` gets translated into something else, possibly ``. If you "right-click > inspect element", you can see how the Expand links end up. Let me know what it is. – Roamer-1888 Oct 13 '14 at 15:06
  • HTML in the question is a part of the page's code, that i get with "inspect element". I checked the possibility, that you mentioned here, and don't see any tracks. I can duplicate the code, and as I understand it ends up with unique number of thread: ` Expand` – Anubelora Oct 13 '14 at 15:55
  • Excellent, that tells me what I need to know. I will post an answer. – Roamer-1888 Oct 13 '14 at 16:01

1 Answers1

0

Based on expand links of the following structure :

<span id="expand_34400795"> <a href="http://*.livejournal.com/1021211.html?thread=34400795#t34400795" onclick="ExpanderEx.make(event,this,'http://*.livejournal.com/1021211.html?threa??d=34400795#t34400795','34400795',true)">Expand</a></span>

try :

$(document).ready(function() {
    $('[id^="expand_"] a').click();
});

In plain English, this jQuery statement reads find all <a> elements in <span>s with ids starting 'expand_', then click the links.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • Thank you for answer. I tried it, but debugger shows me an error `Cannot read property 'click' of null`. jQuery is loaded on page: command `typeof jQuery` gets output `function`. – Anubelora Oct 13 '14 at 20:36
  • I have tried selecting not , but an href with an attribute click: `$('a[onclick*="ExpanderEx"]').click();` - and got same error – Anubelora Oct 13 '14 at 20:44
  • Mmm, very strange because even if the selector selects nothing, an empty jQuery collection will be returned, not null. What does `console.log($());` give? – Roamer-1888 Oct 13 '14 at 21:05
  • What do you get from `jQuery(document).ready(function() { console.log($().jquery); });` ? – Roamer-1888 Oct 14 '14 at 08:20
  • Without any selector I get `TypeError: $(...) is undefined`, with `$('[id^="expand_"] a').click();` - `TypeError: $(...) is null` – Anubelora Oct 14 '14 at 09:04
  • @Anubelora, I think these error conditions are to do with using jQuery in Greasmonkey - an area in which I'm not expert. **[This](http://stackoverflow.com/questions/859024/how-can-i-use-jquery-in-greasemonkey)** seems like a good place to start. – Roamer-1888 Oct 14 '14 at 15:40