简体   繁体   中英

Applescript to extract text in Google Chrome

I need to get a lit of number from Chrome,

The number of item is randoms, so I guess, I need to use a repeat statement

tell application "Google Chrome"
    tell front window's active tab to set MyPersonalID to execute javascript "document.getElementsByClassName('sortable PersonId')[1].innerHTML;"
end tell

This for example is the first number I need (start with ('sortable PersonId')[1] then [2], [3] etc)

also the result of this snippet is

Result:

"

                                    112511721165

                            "

I don't need the random space, so the best option for me would be a result like below :

112511721165, 21241171169, etc...

I tried to get rid of the space with the following script but with no success :

set theText to Unicode text
set theSource to Grabber
property leftEdge71 : "

                                    "
property rightEdge71 : "

                            "
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge71
    set classValue to text item 2 of theSource
    set text item delimiters to rightEdge71
    set purchaseDate to text item 1 of classValue
    set text item delimiters to saveTID
    Result
end try

Many thanks in advance.

UPDATE :

set NumberList to my getInputByClass2("sortable PersonId", 1)


to getInputByClass2(theClass)
    tell application "Google Chrome"
        tell front window's active tab to set r to execute javascript "var outPut=[]; var arr=document.getElementsByClassName('" & theClass & "');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;"
    end tell
    return strings of r 
end getInputByClass2

This get the list of number I want, but still return the 1st result with is a random invalid URL and still have a lot of space in-between the result.

For the first item I use:

items 2 thru -1 of NumberList

but I don't managed to remove the empty space

Since there is no URL to test any of the JavaScript against, this answer is to show how to remove the spaces and linefeeds from the value of the MyPersonalID variable .

In cases like this, I'll often use the do shell script command and a command line utility , as it's just quick and easy me.

Code:

set MyPersonalID to "

                                    112511721165

                            "

set MyPersonalID to do shell script "awk '/[0-9]/{printf $1}'<<<" & quoted form of MyPersonalID

Events:

tell current application
    do shell script "awk '/[0-9]/{printf $1}'<<<'

                                112511721165

                        '"
        --> "112511721165"
end tell
Result:
"112511721165"

If you want to do it in so called pure AppleScript:

Code:

set MyPersonalID to "

                                    112511721165

                            "


my cleanUp(MyPersonalID)


on cleanUp(theText)
    --  # Remove spaces from text.
    set AppleScript's text item delimiters to {" "}
    set theText to text items of theText
    set AppleScript's text item delimiters to {}
    set theText to theText as text
    --  # Remove linefeeds from text
    set AppleScript's text item delimiters to {linefeed}
    set theText to text items of theText
    set AppleScript's text item delimiters to {}
    set theText to theText as text
    return theText
end cleanUp

Events:

Result:
"112511721165"

You get a string from a JavaScript's method, use the JavaScript's trim() method to removes whitespaces from both ends of a string.

Like this: .innerHTML.trim()

Or better: .innerText.trim()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM