Search code examples
javascriptdomapplescript

Using AppleScript to run JS on Brave Settings Pages


I'm trying to get around a limitation that Brave has for extention white/blacklists. I have two conflicting extensions - one I want to run on work sites, and one on pretty much everything else (I've got a list of 800 sites that I want the other to run on).

To do this, Brave won't allow you to blacklist sites for particular extensions - you can either run on all, run on none, or run on specific sites. Therefore, I'm left with a way to add ~800 sites to a whitelist (or ~200 sites to a whitelist if I do it with the work extension instead).

I've had a look through the different ways of doing this in bulk - the settings ui doesn't allow it, manually editing the Secure Preferences file (the only one I've found that has this particular setting in it) just causes the extension to remove itself from Brave.

So I've had a go at using AppleScript to run some Javascript on the page, call the "Add a page" button, and loop through the sites adding it.

The problem I have is that I can't seem to get the button/link via the shadow dom.

It seems to be able to access the page, but I'm not aware of where you need to choose different options within the querySelector to find the right path.

tell application "Brave Browser"
    if not (exists window 1) then reopen
    activate
    tell window 1 to tell active tab
        execute javascript "document.querySelector('extensions-manager').shadowRoot.querySelector('cr-view-manager').shadowRoot.querySelector('extensions-item-list').shadowRoot.querySelector('extensions-detail-view').shadowRoot.querySelector('extensions-runtime-host-permissions').shadowRoot.querySelector('#add-host')"
    end tell
end tell

This just returns "missing value".

The url is brave://extensions/?id={extension id}.

The HTML of the page that I'm targetting is: enter image description here

Anyone know what I'm doing wrong here?


Solution

  • Found a way. See below:

        tell application "Brave Browser"
        tell window 1 to tell active tab
            delay 0.5
            execute javascript "document.querySelector('body > extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view').shadowRoot.querySelector('#container > div > div:nth-child(12) > div.section-content > extensions-runtime-host-permissions').shadowRoot.querySelector('#add-host').click()"
            
            execute javascript "document.querySelector('body > extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view').shadowRoot.querySelector('#container > div > div:nth-child(12) > div.section-content > extensions-runtime-host-permissions').shadowRoot.querySelector('extensions-runtime-hosts-dialog').shadowRoot.querySelector('#input').focus()"
            
            execute javascript "document.querySelector('body > extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view').shadowRoot.querySelector('#container > div > div:nth-child(12) > div.section-content > extensions-runtime-host-permissions').shadowRoot.querySelector('extensions-runtime-hosts-dialog').shadowRoot.querySelector('#input').shadowRoot.querySelector('#input').value = '" & urlValue & "'"
            
            tell application "System Events" to key code 49
            tell application "System Events" to key code 51
            tell application "System Events" to key code 76
        end tell
    end tell
    

    Basically I just used inspector to find the element, copied it to JS (right click on the element in the HTML and you'll see the options), then used that within my Javascript calls.

    The below clicks on the "Add url" button, gets focus on the field, adds some text (urlValue) then sends key codes to first enable the button (space and delete), then enter to trigger it (I couldn't call the click event on the button for some reason).

    Hope this helps someone.