Synchronous Message Passing In Chrome Extensions?
Solution 1:
Unfortunately there isn't. There is a bug report opened about lack of synchronous message passing, maybe if enough people star it they will do something about it.
Solution 2:
You can use the HTML5 File API (navigator.webkitTemporaryStorage or the older deprecated window.webkitStorageInfo with the TEMPORARY option) to store settings to a file. This part is asynchronous but you can do it from background or your options page or popup. This API can change on you since it isn't standardized yet. Since it's temporary storage, your background page may need to run persistent to keep chrome from deleting the file.
Then from your content script you can use synchronous XMLHttpRequest to get the file from "filesystem:" + chrome.extension.getURL("temporary/" + filename). The "filesystem:" prefix on the url is rather important.
Best bet is to use the chrome.storage API (or localStorage) for your settings and hook the onChanged event from that to update the temporary file. If you need to reload the page after a settings change, you'll want to do that in the callback from the FileSystem write operation so you know the content script can see the change.
It doesn't require any special permissions in the manifest. I've personally tested this in chrome 35 but I know it worked in earlier versions as well. I don't know the minimum version requirement, however. I recall seeing a mention of a security change related regression in chrome 33 where it didn't work but was quickly fixed.
I've seen a few other workarounds (e.g. redirect to blob url or data uri from onBeforeWebRequest) for getting settings into content scripts prior to the page's own scripts running but as far as I can tell they've all been intentionally broken in recent versions of chrome due to general XSS and extension security improvements.
Post a Comment for "Synchronous Message Passing In Chrome Extensions?"