0

Is there a way to remove all instance of a class in safari using AppleScript/Javascript ?

For e.g I would like to remove each instance of :

`<td class="sortable cardTypeDisplayName">

                                    Visa

                            </td>`

I suppose that might have something to do with this but I'm lost in the script as I don't have experience with JS

tell application "Safari"
    set RemoveWhatIDontWant to do JavaScript "javascript:Array.from(document.getElementsByClassName('sortable cardTypeDisplayName'))
.forEach(function(v){v.removeAttribute('sortable cardTypeDisplayName');});" in document 1
end tell

UPDATE :

tell application "Safari"
    do JavaScript "document.querySelectorAll("[class=\"sortable cardTypeDisplayName\"]")
        .forEach(el => el.remove());" in tab 1 of window 1
end tell

Result :

Syntax Error Expected end of line but found “[”.

  • stackoverflow.com/questions/17662550/… – user310476 Feb 8 at 16:06
  • @Daniil Importing jQuery just to perform a single operation is overkill, and I don't see any of the the linked solutions resolving the problem here even if jQuery is imported. – grg Feb 8 at 18:16
1
  • .removeAttribute(attrName) takes an attribute name, not value.
    The attribute name is 'class', the value is 'sortable cardTypeDisplayName'.

  • However, that's not going to fix the problem either. Removing the attribute from the element doesn't remove the element entirely, it just removes the attribute, such that your example becomes <td>Visa</td> — i.e. the column still exists.

To remove all elements with a class attribute exactly matching 'sortable cardTypeDisplayName', you can use the following JavaScript.

document.querySelectorAll("[class=\"sortable cardTypeDisplayName\"]")
        .forEach(el => el.remove());

This problem is with your JavaScript, not AppleScript. Debug your JavaScript in the web browser developer tools first before moving to AppleScript.

To run this JavaScript with AppleScript, keep in mind the quotes that need escaping. Look at the syntax highlighting to see how the quotes in the JavaScript are being parsed as quotes for AppleScript to use on the string itself, rather than given to JavaScript. These quotes need escaping.

tell application "Safari" to ¬
  do JavaScript "document.querySelectorAll(\"[class=\\\"module question-stats\\\"]\").forEach(el => el.remove());" ¬
    in current tab of window 1
tell application "Safari" to ¬
  do JavaScript "document.querySelectorAll('[class=\"module question-stats\"]').forEach(el => el.remove());" ¬
    in current tab of window 1
  • Thank you, that's work perfectly within Safari console. I feel stupid for asking, but how can I integrate this with AppleScript, I always get an error when trying to compile (updating my post with my error) – Kevin Feb 8 at 18:58
  • @Kevin Watch the quotes, they need escaping — see edit – grg Feb 8 at 19:34
  • @grg, I'm no JavaScript expert... far from it; however, I'll typically use single-quotes where I can so I do not have to escape the double-quotes... can that be applied here? – user3439894 Feb 9 at 0:34
  • 1
    @user Indeed you can, thanks I've added a version with single quotes, it's still not possible to do away with all the escaping though! – grg Feb 9 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .