Q: Can my Tampermonkey script (or other userscript) call my Chrome extension?
I use Tampermonkey to automate routine tasks in web sites. However, some Chrome APIs (e.g., bookmarks, fileSystem) can only be accessed from a custom Chrome extension.
Tampermonkey does not provide direct access to the Chrome API, so I am looking for a workaround. My idea was to make a simple Chrome extension that does those things, and call it from my Tampermonkey script, which would still still contain the rest of the logic.
Ideas
(Admittedly, I have not yet tried these, yet.)
- Cross-extension messaging allows separate extensions to communicate via chrome.runtime.sendMessage(…). I don't know how/if that can be used with Tampermonkey.
- Could window.postMessage(…) be used to send a message using the tab itself?
- Create a local web application that passes messages to one from the other via XHR.
Security
These scripts are not distributed publicly, and are restricted to specific sites. Thus, the risks that have led Tampermonkey to not include access to those APIs are less significant in this context.
Related posts
- 2019-04-22: Can TamperMonkey create a bookmark?
- 2018-09-25: Can Tampermonkey Userscript access Chrome API?
- 2016-04-16: Is it possible to execute another chrome extension using your own chrome extension?
- 2016-12-13: TamperMonkey - message between scripts on different subdomains
- 2012-03-28: Chrome extension code vs Content scripts vs Injected scripts