tarampampam / random-user-agent

😎 Browser extension that automatically replaces the User-Agent with a randomized one
https://random-user-agent.com
MIT License
571 stars 74 forks source link

Support for Safari web extension #573

Open ajimix opened 4 months ago

ajimix commented 4 months ago

Is there an existing issue for this?

Describe the problem to be solved

Safari now supports web extensions, so it should be fairly easy to convert the current extension to Safari

Suggest a solution

No response

Additional context

No response

ajimix commented 4 months ago

I managed to convert it with one single command:

xcrun safari-web-extension-converter random-user-agent --macos-only

And you can see the popup automatically works:

a

Unfortunately the user agent is not being changed, I'm still researching how to make it work

ajimix commented 4 months ago

After some research, it looks like Safari does not support modifying request headers. So this extension will not be able to work as the extension modifies the request headers.

I found a "solution" to quickly switch user agents using an Apple script. I'm leaving it here in case it is useful for someone. Note that this only changes it for the current website.

set userAgents to {¬
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36", ¬
    -- ...add more user agents here as you like
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0"}

set randomIndex to random number from 1 to length of userAgents
set selectedUserAgent to item randomIndex of userAgents

tell application "Safari" to activate
tell application "System Events"
    tell process "Safari"
        click menu item "Other…" of menu "User Agent" of menu item "User Agent" of menu "Develop" of menu bar item "Develop" of menu bar 1
        keystroke "a" using {command down}
        keystroke selectedUserAgent
        keystroke return
    end tell
end tell

So, each time you run this apple script, the user agent will be set to one random in the array. It is not as good as this extension but better than nothing

ajimix commented 4 months ago

Looks like it's possible to do, this developer has created an open source extension that does that. You can see the background.js code here: https://github.com/katagaki/Unagent/blob/main/Extension/Resources/background.js

ajimix commented 4 months ago

@tarampampam I've been testing this code: https://github.com/katagaki/Unagent/blob/main/Extension/Resources/background.js and it works properly in Safari. So I think we only need to modify the code of your extension to run this code instead if the browser is Safari and the full extension can be easily converted using the xcrun command I shared above.

I wanted to work on it and maybe submit a PR but then I saw you have been working on v4 in #572 So maybe you can implement the safari functionality for v4. What do you think? let me know if I can help in some way

tarampampam commented 4 months ago

Hi there and thank you for your contribution! Could you try the same with the fresh alpha build?

ajimix commented 4 months ago

Hi there and thank you for your contribution! Could you try the same with the fresh alpha build?

@tarampampam I tested it with the released 4.0.1, and the popup is working properly, take a look at the screenshot below.

The only missing part is to add this user agent replacement code for Safari instead of the current code and then do some testing and release it (you can probably even sell it on the app store for 2.99 or 4.99 or similar, as it's quite common for open source Safari extensions to be sold on the app store)

screenshot

tarampampam commented 4 months ago

Unfortunately, I've been a Linux user for many years and don't have anything macOS-based at hand. This is a blocker for checking everything and preparing a release for the Safari Extensions Store :(

digitalloophole commented 2 months ago

@ajimix Would you consider forking and adding your code so other Safari user can easily use this extension?

ajimix commented 2 months ago

@digitalloophole unfortunately not as that would require maintaining the fork as well, to keep it up with the project changes and currently I don’t have the time for it. But maybe someone who has the time can do it