Wednesday, May 18, 2022

#1 2022-05-07 05:11:46 am

istarhan
Member
Registered: 2020-03-20
Posts: 7

How to expand all collapsible sections on a web page for text search

Hello everyone,

I have been using an internal website with multiple pages that are designed to store information under collapsible sections. In order to find what I look for, I need to expand all sections to perform a Cmd+f text search.

Chrome's "Inspect element" feature indicates that collapse triangles are tied to a "class=collapsed" item.

I would like to create a script that expand all collapsed text on a web page.

Any help would be much appreciated.

Regards,

Offline

 

#2 2022-05-07 01:06:21 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 545

Re: How to expand all collapsible sections on a web page for text search

I'm guessing this is going to be difficult for us to help you with without an example page.

Also, the solution would be Javascript; you'd probably have more luck getting an answer on SuperUser or somewhere.

If it were me, I'd try grabbing the page source and just run the search on that.

I don't know what you're doing with the text you're searching for. But something like this:

[applescript]tell application "Google Chrome"
    set source to execute front window's active tab javascript "document.documentElement.outerHTML"
    if source contains "whatever" then return true
end tell[/AppleScript]

Maybe that's not going to work for you. If you really do need to expand the page, I suspect it's possible, but again, hard for us to work on with no sample page.

Last edited by t.spoon (2022-05-07 01:07:16 pm)


Ditched the Hackintosh.
Intel Mac Mini i7 and M1 Mac Mini on a KVM.

Offline

 

#3 2022-05-08 01:47:11 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 955

Re: How to expand all collapsible sections on a web page for text search

I do not have a solution for you but here is 3 approaches I have seen.

1. Get the text contents of the URL and do search on that (AppleScriptObjC could do that, fast)

2. Get the text contents of the URL and use python (package: beautiful-soup)
https://beautiful-soup-4.readthedocs.io/en/latest/

3. Use JavaScript with tell block of a scriptable web browser

1, 2) fast
3) If you ask me much more complicated and slow

Take a lock here
https://stackoverflow.com/questions/442 … ext-search


if you are the expert, who will you call if its not your imagination.

Offline

 

#4 2022-05-14 02:08:50 am

istarhan
Member
Registered: 2020-03-20
Posts: 7

Re: How to expand all collapsible sections on a web page for text search

Thank you @frederik71  and @t.spoon for your responses.
Unfortunately, I do not have control over editing page coding and since it is an internal page I can not share it.
Therefore the only option for me seems to locate page elements via a script command and perform a click action for all.
However, to elaborate more

https://support.apple.com/guide/iphone/welcome/15.0/ios

This page has a quite similar strucure.

If you click "table of contents" you would see a pop-up list that contains multiple collapsible items.

What I need to do is to expand all collapsed items at once with a script.

Regards,

Offline

 

#5 2022-05-14 02:30:40 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2276

Re: How to expand all collapsible sections on a web page for text search

istarhan wrote:


https://support.apple.com/guide/iphone/welcome/15.0/ios

This page has a quite similar strucure.


This page has links. Move the mouse over each link, perform right click to copy its URL. No, you can open this "collapsable" thing which is link using open location command:

For example, link Table of Contents has URL "https://support.apple.com/en-gb/guide/iphone/toc/15.0".

So, simply open this URL:

Applescript:


open location "https://support.apple.com/en-gb/guide/iphone/toc/15.0"

Opening URL of link iPhone 13 mini:

Applescript:


open location "https://support.apple.com/en-gb/guide/iphone/iph7d116e557/15.0/ios/15.0"

You can get all embedded links (URLs) programatically as well. Open webpage, run script:

Applescript:


set theDocumentLinks to "function documentLinks() {
//
var arr = [], links = document.links;
for(var i = 0; i < links.length; i++) {
   arr.push(links[i].href);
}
return arr
}
//
documentLinks()
"


tell application "Safari" to set theLinks to do JavaScript theDocumentLinks in document 1

Now, you can open this links one by one. Or, use repeat loop. NOTE: if the page has too many embedded URLs, opening all of them in the loop is bad idea. This will overflow the browser cash.

Last edited by KniazidisR (2022-05-14 02:52:55 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2022-05-14 09:37:18 am

istarhan
Member
Registered: 2020-03-20
Posts: 7

Re: How to expand all collapsible sections on a web page for text search

Thank you @KniazidisR

However, I am not trying to open the URL of "Table of contents".

When you click that you will see a pop-up window

WgRTiu.md.png

What I need to do is to expand all collapsed items at once on such a page.

For instance, this list has a "Set up and get started" collapsed item and under this item, there is an "Apple ID and iCloud" collapsed item.

I want Applescript to detect all collapsed items and expand all at once so that I can perform a Cmd+f search, since without expanding all collapsed items a Cmd+f search with "Apple id" would return "0".

Regards,

Last edited by istarhan (2022-05-14 10:04:09 am)

Offline

 

#7 2022-05-14 12:50:15 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2276

Re: How to expand all collapsible sections on a web page for text search

Open manually the Table of Contents, run following script.

Applescript:


tell application "Safari"
   activate
   tell document 1
       do JavaScript ("document.getElementsByClassName('section').length")
       set sectionsCount to result as integer
       repeat with i from 0 to (sectionsCount - 1)
           do JavaScript ("document.getElementsByClassName('section')[" & i & "].click();")
       end repeat
   end tell
end tell

Last edited by KniazidisR (2022-05-14 01:26:07 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2022-05-16 02:44:30 pm

istarhan
Member
Registered: 2020-03-20
Posts: 7

Re: How to expand all collapsible sections on a web page for text search

Thank you @KniazidisR that works like a charm after I adapted the class name.

Kindest regards,

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)