8

When i console.log(chrome) with google chrome browser i get certain properties but i find the 'runtime' property of chrome is not available.

app: Object
csi: function () { native function GetCSI(); return GetCSI();}
loadTimes: function () { native function GetLoadTimes(); return GetLoadTimes();}
webstore: Object
__proto__: Object
__defineGetter__: function __defineGetter__() { [native code] }
__defineSetter__: function __defineSetter__() { [native code] }
__lookupGetter__: function __lookupGetter__() { [native code] }
__lookupSetter__: function __lookupSetter__() { [native code] }
constructor: function Object() { [native code] }
hasOwnProperty: function hasOwnProperty() { [native code] }
isPrototypeOf: function isPrototypeOf() { [native code] }
propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
get __proto__: function __proto__() { [native code] }
set __proto__: function __proto__()

so chrome.runtime is undefined.

and hence i am not able to use chrome.runtime.sendMessage for my extension

How to resolve the above??

EDIT :

my code is :

    if(typeof(chrome) === 'undefined'){
                result.isChromeBrowser = false;
                return next(result);
              } else {
                result.isChromeBrowser = true;
              }

console.log(chrome.runtime);  // undefined
    
              //check whether the chrome runtime is available or not ...
              if(!chrome.runtime){
                result.isChromeRuntimeAvailable = false;
                console.log(result);
              } else {
                result.isChromeRuntimeAvailable = true;
              }

EDIT 2 :

from here : https://developer.chrome.com/docs/extensions/mv3/manifest/externally_connectable. I am sure(correct me if i am wrong after going through above link) that a web page can communicate with a chrome extension. But not able to make it up through when the extension is installed from chrome store, however working perfectly in case of extension installed from local directory.

i am providing externallyConnectable as :

"externally_connectable": {
        "matches": [
            "*://local.mywebsite.com/*"
        ]
    }

I have included the externally_connectable with "matches" property.. Now when i load unpacked directory to install extension, my web page get chrome.runtime.. but when i install extension from chrome store, the same web page on same browser does not get chrome.runtime.. why so?? in the end i still dont have chrome.runtime on the page ://local.mywebsite.com/. help me out.

gignu
  • 1,763
  • 1
  • 14
  • 24
codeofnode
  • 18,169
  • 29
  • 85
  • 142
  • 1
    Are you using the extension's console or are you still in the page context? – 1337holiday Feb 12 '14 at 06:53
  • from the page context. – codeofnode Feb 12 '14 at 06:58
  • 2
    In console you will see a dropdown with ``, change that to your extension `chrome-extension:`, then run chrome.runtime. To find your extension id go to `Tools > Extensions` – 1337holiday Feb 12 '14 at 07:01
  • However of course we need extension id to send the message.. but i didn't get do we really need extension id here to get chrome.runtime not equals to undefined ..?? plz go through edit of questions – codeofnode Feb 12 '14 at 07:06
  • 1
    The problem is that you are executing `chrome.runtime` in the wrong console. My previous comment shows how to get the correct console. – 1337holiday Feb 12 '14 at 07:07
  • I have been using web application (page context) to get chrome.runtime many times earlier and i easily found chrome.runtime. For the current senario, on same browser, I am executing chrome.runtime in page context that my web application page.. NOT in background page of extension. Am i using correct console? – codeofnode Feb 12 '14 at 07:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47291/discussion-between-1337holiday-and-koka) – 1337holiday Feb 12 '14 at 07:12
  • @Koka did you include your site in the `externally_connectable` section of your manifest? https://developer.chrome.com/extensions/manifest/externally_connectable.html – rsanchez Feb 12 '14 at 13:17
  • thanku so much for pointing out my mistake.. to include my url under externally_connectable.. – codeofnode Feb 12 '14 at 14:19
  • I have included the externally_connectable with "matches" property.. Now when i load unpacked directory to install extension, my web page get chrome.runtime.. but when i install extension from chrome store, the same web page on same browser does not get chrome.runtime.. why so?? – codeofnode Feb 14 '14 at 07:48
  • @Koka Link to extension in CWS? – Rob W Feb 21 '14 at 09:42
  • Extension is privately shared and can't be exposed to public now. Can you please dig it without extension and extension-id. I am here to answer all doubts and questions. – codeofnode Feb 21 '14 at 11:27
  • Maybe the extension is not the same version as the unpacked directory and lack the externally_connectable. It seems weird that only your unpacked works. Try another chrome maybe. Also don't forget that if in your extension you use "runtime.onStartup()" you need to COMPLETELY restart chrome (it may still linger on a background process and needs to be task managered). – AlexandruB Feb 26 '14 at 18:42
  • Thanks to all of you for helping me out. – codeofnode Feb 27 '14 at 11:53
  • If it does not exist, could you create a new one? The Mozilla docs include some reference material: https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_NewRuntime – ThorSummoner Sep 23 '14 at 19:19

2 Answers2

2

My problem get to solved by removing the plugin completely from chrome store and re-upload and re-publish the plugin again.

The problem was : Initially i did not had 'externally_connectable' property, so wasn't able to get chrome.runtime defined. Later when i included, then i was updating the chrome plugin. And the main cause may be : 'Chrome store does not modify the 'manifest.json' (at least for certain properties like 'externally_connectable') just by updating the plugin by uploading. You may have to remove and re-upload to get manifest.json updated' (This is what i can conclude because of my experience, Please correct me if i am wrong with some valid reference source.)

and so 'chrome.runtime' remains undefined.

Later when i removed the plugin and re-uploaded, everything worked fine.

codeofnode
  • 18,169
  • 29
  • 85
  • 142
  • 1
    The store should update the whole `manifest.json` when you upload a new version, but your local Chrome installation won't get updated instantly. You can force it by going to chrome://extensions/, enabling Developer Mode, and clicking "Update extensions now". It's often better to iterate on an extension by loading it "unpacked", and then only upload it to the store when it's working. – Jeffrey Yasskin Mar 24 '14 at 00:38
1

You might have this issue which was previously already resolved:
chrome.runtime.sendMessage not working as expected

Try to check what sendMessage is availaible. If none are, then the chrome version is really old:
Chrome Extension: Port error: Could not establish connection. Receiving end does not exist.

Hope I helped, cheers!

Community
  • 1
  • 1
AlexandruB
  • 678
  • 3
  • 15
  • link above has the problem that 'sendMessage is not a function'. But mine status is 'runtime is available'. Regarding version. I am saying all works fine for the local but the problem is when i install plugin from chrome store. Means i hope there is no chrome version related issue as i have the latest one. – codeofnode Feb 26 '14 at 07:07