ViewPort Text IE

Post your working scripts, libraries and tools
User avatar
tank
Posts: 2830
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

ViewPort Text IE

25 Dec 2019, 20:11

I am certain someone has done this before and if not i dont understand why not

I recently was asked to get only the visible text from IE.
Funny i thought it was easy too :headwall:

Apparently the only good way to do it is with jQuery so lets cover the jQuery only portion
google it anywhere and you will see dozens of suggestions for using the selector :visible .... :roll: If only :facepalm:

after much much frustration i landed on this selector in a stack overflow page
*:not(:has(*)):visible

implementing it looked like this $("*:not(:has(*)):visible").text() :dance:

Now the easy part what if jQuery is not globally accessible

Code: Select all

            if ((typeof $) != "function"){                                      // if jQuery is not global accessible

                script          = document.createElement("script");             // create a script tag
                script.type     = "text/javascript";                            // set to javascript
                script.src      = "//code.jquery.com/jquery-2.1.1.min.js";      // without protocol to prevent security conflict
                script.async    = true;                                         

                document.getElementsByTagName("head")[0].appendChild(script);   // add the element
            }
and then it should have been as easy as
var someoutputvar = $("*:not(:has(*)):visible").text()
and retrieve as document.parentWindow.someoutputvar but at least on my system COM cannot any longer access global objects without settings changes. :headwall: :headwall: :headwall: To work around this i added a hidden element

Code: Select all

            div                     = document.createElement("div");                // Create a div
            div.id                  = "uniqueID";                                    // unique id
            div.style.display       = "display:none";                               // make sure it isnt visible

            document.getElementsByTagName("body")[0].appendChild(div);              // Add it to the body
then output the viewport text like this

Code: Select all

document.querySelector("#uniqueID").innerText = $("*:not(:has(*)):visible").text()
anyway this is what i came up with

Code: Select all

viewport(pdoc){
    DivID               := "SpecialOutput" A_NowUTC
    js = 
    (
        if ((typeof $) != "function"){                                      // if jQuery is not global accessible

            script          = document.createElement("script");             // create a script tag
            script.type     = "text/javascript";                            // set to javascript
            script.src      = "//code.jquery.com/jquery-2.1.1.min.js";      // without protocol to prevent security conflict
            script.async    = true;                                         

            document.getElementsByTagName("head")[0].appendChild(script);   // add the element
        }
        div                     = document.createElement("div");            // Create a div
        div.id                  = "%DivID%";                               // unique id
        div.style.display       = "display:none";                           // make sure it isnt visible

        document.getElementsByTagName("body")[0].appendChild(div);          // Add it to the body
        document.querySelector("#%DivID%").innerText = $("*:not(:has(*)):visible").text()
    )
    
    try pdoc.parentWindow.eval(js)
    catch e {
        pdoc.parentWindow.execScript(js)
    }
    return document.querySelector("#" DivID).innerText
    }
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Koopa8840, Mesvir and 16 guests