Showing all the comments on Stackexchange site automatically Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Showing all the comments on Stackexchange site automatically

27 Jul 2021, 17:48

I'm using autohotkey to open all the sites on a specific page of https://math.stackexchange.com/ by using the code which @teadrinker wrote:

Code: Select all

ignored =
(
;list of my ignored tags
)
objIgnored := {}
Loop, parse, ignored, `n, `r
   objIgnored[A_LoopField] := ""

url := "https://math.stackexchange.com/questions"

Whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
Whr.Open("GET", url, false)
Whr.Send()
status := Whr.status
if (status != 200)
   throw "HttpRequest error, status: " . status

Arr := Whr.responseBody
pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
length := arr.MaxIndex() + 1
html := StrGet(pData, length, "UTF-8")

Doc := ComObjCreate("htmlfile")
Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
Doc.write(html)

summary := Doc.querySelectorAll("div.summary")
links := ""
Loop % summary.length {
   tags := summary[A_Index - 1].querySelectorAll("div.tags > a.post-tag")
   Loop % tags.length
      if objIgnored.HasKey( tags[A_Index - 1].innerText )
         continue 2
   Run, % link := StrReplace(summary[A_Index - 1].querySelector("a.question-hyperlink").href, "about:", "https://math.stackexchange.com",, 1)
   links .= link . "`n"
}
MsgBox, 4096,, % links ; this line to check which links were found and opened
Return
It opens all the questions on my browser perfectly. but I have an issue. some posts on the site have lots of comments below the question and answer and in order to view all of them I have to click on "Show ... more comments" ( "..." is the number of comments). For example in this post https://math.stackexchange.com/questions/4208219/solve-int-0-pi-6-sec3-theta-mathrm-d-theta# we can see "Show 8 more comments" which I should click on that in order to show.

Is it possible to add some line of codes to the above script in order to show all the comments below each question and answers automatically? (Doing this will help me a lot because I am opening all questions then go disconnect and read them offline.)

Thanks in advanced
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Showing all the comments on Stackexchange site automatically

27 Jul 2021, 19:57

soheill0098 wrote: Is it possible to add some line of codes
Nope, AHK cannot directly control the web browser (except for Internet Explorer).
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

27 Jul 2021, 20:35

teadrinker wrote:
27 Jul 2021, 19:57
soheill0098 wrote: Is it possible to add some line of codes
Nope, AHK cannot directly control the web browser (except for Internet Explorer).
Oh! Does that mean Internet Explorer has more features than for example Microsoft edge?! Then why is it so underrated?
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

27 Jul 2021, 20:49

Yes, sure it's possible. You do an image search for the image "more comments" on the page. You know the x coordinate of the text, so the search area is not so big and it should be quite quick (you may need to do a {pgdn} if you don't find the image on the first page. Then you just have to click on the found coordinates before saving the page to a file.

Maybe you don't even need an imagesearch. Got to the end of the page, then {pgup}, and I think you will find the "show x more comments" always at the same place. Use window spy to find the coordinates, they will vary depending on your browser, on the font size etc., but if these parameters are not changed, the position should always be the same.
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

27 Jul 2021, 22:46

braunbaer wrote:
27 Jul 2021, 20:49
Yes, sure it's possible. You do an image search for the image "more comments" on the page. You know the x coordinate of the text, so the search area is not so big and it should be quite quick (you may need to do a {pgdn} if you don't find the image on the first page. Then you just have to click on the found coordinates before saving the page to a file.

Maybe you don't even need an imagesearch. Got to the end of the page, then {pgup}, and I think you will find the "show x more comments" always at the same place. Use window spy to find the coordinates, they will vary depending on your browser, on the font size etc., but if these parameters are not changed, the position should always be the same.
I'm not an expert in ahk, so can you please do it? I use Microsoft edge browser and %100 zoom. Thanks!
By the way, I don't think the coordinate is the same on each post. Because some posts have "Show x more comments" in the question or some answers and some of them have not that many comments.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 01:02

soheill0098 wrote: Does that mean Internet Explorer has more features than for example Microsoft edge?!
Of course not. But since IE is a part of Windows it's easy to control it.
braunbaer wrote: Yes, sure it's possible. You do an image search for the image "more comments" on the page.
I'm not sure if it's that simple.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 04:00

Create Your own edge extension, that will open comments or use tampermonkey.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 08:20

I'll have a look at it as soon as possible.
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 08:53

braunbaer wrote:
28 Jul 2021, 08:20
I'll have a look at it as soon as possible.
Thanks! I have an idea, maybe using Ctrl + F and searching for a phrase like "more comment" would help to first find out how many items it need to be clicked and also maybe it helps to find the coordinates better.

I found https://math.stackexchange.com/questions/733754/visually-stunning-math-concepts-which-are-easy-to-explain has 6 "more comment" so it is perfect to work with!
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 09:39

I just wrote this script based on my previous post reply:

Code: Select all

^!a::
send, ^f
sleep,100
send, more comment
sleep,100
loop,6
{
send, {Enter}
sleep,1000
Click,-300,416
sleep,2000
click, 1026,98
sleep,1000
}
return

Esc::ExitApp
But a problem with this code is that, when there are for example 2 section of "more comment" it doesn't stop and it continue clicking! To fix this, I have to match the the count item of my loop to number of search result. But not sure how I can do it.
Last edited by soheill0098 on 28 Jul 2021, 10:32, edited 1 time in total.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 10:11

Now I see the problem. It is not the "n" comments, you just have to click on this text one time to show all n comments.
But if there are a lot of answers, these answers are distributed over several pages. And the number of pages have nothing to do with the number of "more comments" - and the "show more comments" clickable text appears under more than one answer.

That's really tricky.
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 10:23

Now I see the problem. It is not the "n" comments, you just have to click on this text one time to show all n comments.
Yes Exactly!
But if there are a lot of answers, these answers are distributed over several pages. And the number of pages have nothing to do with the number of "more comments" - and the "show more comments" clickable text appears under more than one answer.
It is very rare for the site to have that many answer to be on different pages in fact the link I sent was one of the most popular questions on the site. So we consider there is only one page for each post. so fortunately it is not a problem here.

In fact the number of " Show more x comments" would be at most 2 or 3 normally!
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 11:24

If the script needs only to work on questions with all answers on a single page, it should be relatively easy to implement. The number of comments is irrelevant, as it is enough to make one click to show all comments, no matter how many there are.
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 11:29

braunbaer wrote:
28 Jul 2021, 11:24
If the script needs only to work on questions with all answers on a single page, it should be relatively easy to implement. The number of comments is irrelevant, as it is enough to make one click to show all comments, no matter how many there are.
Yes, there is only one page contain question and all the answers.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 12:06

Just working on it, but I will need a while. It's not a one-liner :)
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 12:23

One way is to run js from the address bar and this will click the more answers

Code: Select all

document.getElementsByClassName("js-show-link")[0].click()
You could run a loop in JS and it will open all of them if there is more than one
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Showing all the comments on Stackexchange site automatically

28 Jul 2021, 13:26

I have not yet used imagesearch, looks like it does not work at all.

I used the windows 10 snipping tool to download the picture:
MoreComments.GIF
MoreComments.GIF (1.18 KiB) Viewed 543 times
From this page:
Not found.GIF
Not found.GIF (20.35 KiB) Viewed 542 times
It does not find anything, I tried gif and jpg, increased the variation to 10, searched the whole screen:

Code: Select all

        imagesearch x,y,  100, 100 , 1000, 1080, *10 MoreComments.gif
        if !ErrorLevel
            msgbox 1,, found: %x% %y%
        else msgbox, 1,, Not found, EL=%Errorlevel%
soheill0098
Posts: 69
Joined: 01 Sep 2020, 12:28

Re: Showing all the comments on Stackexchange site automatically  Topic is solved

13 Aug 2021, 20:50

I could finally find a solution for it. I used the script by the user @Shadow Wizard Wearing Mask V2 in this post https://meta.stackexchange.com/questions/289105/expand-all-comments-button. And for doing this automatically I wrote this script

Code: Select all

^!p::send,+^i
sleep,3000
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := ""           ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
clipboard =               ; copy this text:
(
{function TriggerSingleLink(links, index) {
    if (index >= links.length) {
        console.log("All done.");
        return;
    }
    var oLink = links[index];
    oLink.trigger('click');
    window.setTimeout(function() {
        TriggerSingleLink(links, index + 1)
    }, 500);
}
var links = [];
$('.js-show-link').each(function() {
    var oLink = $(this);
    if ($.trim(oLink.text()).length > 0)
        links.push(oLink);
});
if (links.length > 0) {
    console.log("Expanding " + links.length + " link" + ((links.length > 1) ? "s" : "") +"...");
    TriggerSingleLink(links, 0);
} else {
    console.log("No valid expand links found.");
}
}
)
ClipWait, 2              ; wait max. 2 seconds for the clipboard to contain data. 
if (!ErrorLevel)         ; If NOT ErrorLevel, ClipWait found data on the clipboard
    Send, ^v             ; paste the text
Sleep, 300
clipboard := ClipSaved   ; restore original clipboard
ClipSaved =    ; Free the memory in case the clipboard was very large.
sleep,500
send,{Enter}
sleep,100
send, {F12}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, Rohwedder, ruespe and 382 guests