4 ms·
Show HN: Sniper: A Manifest V3 web extension for dynamic user specified actions
This extension makes use of JS computed property names to perform user specified dynamic actions on user specified elements.
I started working on an extension primarily for my own very specific use case. I knew of Tampermonkey and it's relatives before, but hadn't used it extensively. I also was following the news of MV3, so wasn't sure of their long term viability. But more than anything having recently got into frontend development I also just wanted to build an extension myself, getting to understand the newer limitation and alternatives was just a bonus point.
Literally a couple of days ago I got to know (From HN nonetheless https://news.ycombinator.com/item?id=38526277 https://news.ycombinator.com/item?id=38526277) that userscripts are going to be allowed in MV3 too, so I finally decided to actually check out ViolentMonkey, which is pretty neat, but from the looks of it would have to migrate to `chrome.userScripts.register` which would eventually require `userScripts` permission and with it would need [developer mode enabled](https://developer.chrome.com/docs/extensions/reference/api/userScripts#developer_mode_for_extension_users https://developer.chrome.com/docs/extensions/reference/api/u...). While browsing through the subsequent discussions I saw there were many other alternatives for dynamic script execution, from creating and the dynamic code to `<script>` tags to using `evaljs`, but I wasn't aware of them while building this (see for ex: https://news.ycombinator.com/item?id=31425256 https://news.ycombinator.com/item?id=31425256 and https://github.com/Tampermonkey/tampermonkey/issues/644#issuecomment-857838249 https://github.com/Tampermonkey/tampermonkey/issues/644#issu...) (and tbf it wan't even my goal to get full JS execution in my extension).
Long term my goal was to build a small JSON config for the actions needed and parse and apply them to have the desired behavior. I also was planning on exposing some extension only behavior (like tab functionality) via message passing with service workers (The config could be something like
```
{
"selectors": ['.container h1', 'h2'],
"actions" : [
{
"action": "setAttribute",
"args": ["style", "color:green"]
}...
]
}
```
)
But now given that MV3 will support userScripts and since devmode is not a deal breaker for me, I plan to use violent monkey itself. I'm sharing this code for anyone else who might be find it useful.
P.S: For installation you have to load unpacked extension in your browser, since I haven't uploaded it to any extension marketplace and for firefox users the extension makes use of [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) for dispatching content scripts config from service worker which unfortunately is not yet supported, so it might not work as expected.