// It turns out that getManifest() returns undefined when the runtime has been
// reload through chrome.runtime.reload() or after an update.
function isValidChromeRuntime() {
// It turns out that chrome.runtime.getManifest() returns undefined when the
// runtime has been reloaded.
// Note: If this detection method ever fails, try to send a message using
// chrome.runtime.sendMessage. It will throw an error upon failure.
return chrome.runtime && !!chrome.runtime.getManifest();
// E.g.
if (isValidChromeRuntime()) {
chrome.runtime.sendMessage( ... );
} else {
// Fall back to contentscript-only behavior
}
// Content script
function main() {
// Set up content script
function destructor() {
// Destruction is needed only once
document.removeEventListener(destructionEvent, destructor);
// Tear down content script: Unbind events, clear timers, restore DOM, etc.