Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communicating with script injected into webpage. #47

Closed
godd9170 opened this issue Jan 14, 2017 · 16 comments · Fixed by #49
Closed

Communicating with script injected into webpage. #47

godd9170 opened this issue Jan 14, 2017 · 16 comments · Fixed by #49
Assignees

Comments

@godd9170
Copy link
Contributor

Hey @tshaddix

Really impressed with this project, as I see some huge potential in the world of 'web-app augmentations'.

I'm trying to make use of react-chrome-redux through a script I've injected into a webpage from a chrome extension. I'm using https://github.com/KartikTalwar/gmail-chrome-extension-boilerplate/blob/master/main.js to get ahold of some gmail controls, and in order to do so, you have to inject your .js in to the page's header. Running plain React works great this way, but I also want a common redux store in the background. Attempting a similar workflow to your clicker-key example, is when I run into a snag:

chrome.runtime.connect() called from a webpage must specify an Extension ID (string) for its first argument

It seems communicating to an extension is possible, and it totally stands to reason that you need to tell chrome which one... I'm just not really sure how to do that. Is this within the scope of the project/Can you point me in the right direction?

@godd9170
Copy link
Contributor Author

I think it might be as simple as adding in the extension id as the first parameter here: https://github.com/tshaddix/react-chrome-redux/blob/master/src/store/Store.js#L18

Would making an optional 'Extension Id' param to proxyStore make this work? I'll give it a shot once I get a chance.

https://developer.chrome.com/extensions/runtime#method-connect

@tshaddix
Copy link
Owner

Hi there, @godd9170!

I'm thrilled to hear you enjoy this package!

I've definitely seen that error before... Is the content script you're communicating with owned by the same extension that is holding your background store?

@godd9170
Copy link
Contributor Author

godd9170 commented Jan 15, 2017 via email

@tshaddix
Copy link
Owner

@godd9170 Hmmm that's very interesting.

If the content script is owned by the same extension as the background page, the extension id should be optional according to the chrome docs.

The ID of the extension or app to connect to. If omitted, a connection will be attempted with your own extension. Required if sending messages from a web page for web messaging.

So I'm not sure why you're getting that error. Very odd. Do you mind posting some code where you instantiate the store in the background and the proxy store in the content script?

@tshaddix
Copy link
Owner

Ahhhh now I get it:

your .js in to the page's header

So your code is actually using web-messaging! Your call is coming from the webpage itself, not the content script owned by the extension, right?

Yes, I think we should allow a variable in the Store options to take an extension id. Do you want to make a PR?

@tshaddix
Copy link
Owner

If not, I'll be happy to. Feel free to blame me for everything!

@godd9170
Copy link
Contributor Author

godd9170 commented Jan 15, 2017 via email

@tshaddix
Copy link
Owner

Sounds great! Happy to help wherever you need it!

@godd9170
Copy link
Contributor Author

Ok I've taken a run at this and come up short. Here are my modifications to react-chrome-redux

godd9170@60f3ef0

And here is how I'm trying to implement it as a variant of your clicker-key example.

godd9170/clicker-key@8ee8dd4

It seems like the messages just aren't being received in background.js. I'm trying the 'web-messaging' approach, added in the 'externally_connectable' bits to the manifest and no luck. The page errors when it tries to get the response from sendMessage (https://github.com/godd9170/react-chrome-redux/blob/master/src/store/Store.js#L74).

Thoughts?

@godd9170
Copy link
Contributor Author

Ok so this is likely why... you've got to receive the messages differently

(https://developer.chrome.com/extensions/messaging#external-webpage)

chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (sender.url == blacklistedWebsite)
      return;  // don't allow this web page access
    if (request.openUrlInEditor)
      openUrl(request.openUrlInEditor);
  });

Would adding the onMessageExternal listener that is essentially a copy of the regular onMessage be out of scope/break the package?

@tshaddix
Copy link
Owner

Fixed in #49

@villageitservices
Copy link

My work is not related to this rep. But it is a chrome extension too. I wasn't sure why I had to use onMessageExternal even though the content script and background script are from the same extension.Thanks for your help @godd9170

@MrBlenny
Copy link

MrBlenny commented Nov 29, 2017

What is the status of this? I'm sufferting the same issue when trying to send messages through a script that I have injected into the page. Adding the extensionId fixes the first issue, now I have an issue with sendMessage because resp is not defined.

chrome.runtime.sendMessage(_this3.extensionId, {
          type: _constants.DISPATCH_TYPE,
          portName: _this3.portName,
          payload: data
        }, function (resp) {
          var error = resp.error,
Error in event handler for (unknown): TypeError: Cannot read property 'error' of undefined

@vhmth
Copy link
Collaborator

vhmth commented Dec 11, 2017

@MrBlenny you mean that you can't connect normally from a script hosted/served from your extension? Do you have some minimal code example I can run to check this out?

@MrBlenny
Copy link

The error only seemed to occur when the page does not have externally_connectable.matches set in the manifest. I resolved this issue by manually sending a message using chrome.runtime.sendMessage and only adding the proxy store if the chrome.runtime.sendMessage gets a response.

@vhmth
Copy link
Collaborator

vhmth commented Jan 4, 2018

@MrBlenny you shouldn't be able to send a message from a webpage to an extension without a externally_connectable matches entry. From the docs:

If externally_connectable is not declared in your extension's manifest, all extensions and apps can connect, but no webpages can connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants