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 “[”.
Best Answer
.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.
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.