2

Since injected script can't do cross domain xmlhttp requests I tried, as it is advised in the manuals to send message from injected script to background script for it to do the job. But I cannot get the messages to work.

This is my code, it is only three files, as simple extension as it could be.

config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" id="http://www.example.org/helloworld">
  <name>postMessage-testcase</name>
  <description>check if opera's postMessage fails</description>
</widget>

index.html

<!doctype html>
<html lang="en">
<head>
<script>
  opera.extension.onmessage = function(event) {
    alert("get the message!");
    opera.extension.postMessage("posting reply");
  };
</script>
</head>
<body></body>
</html>

includes/user.js

// ==UserScript==
// @include     *apod.nasa.gov*
// ==/UserScript==

opera.extension.onmessage = function(event) {
  alert("got reply !");
};

alert("yep, I'm on apod, proceeding with messages");
opera.extension.postMessage("message body");

The script should send back and forth messages when user opens apod.nasa.gov, but only one msg is send here, no messaging is taking place, also no error show up in the console.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • Why not just use the HTML5 web workers API? Looks pretty much the same thing as what you're doing, and definitely works in Opera 11.6+. – Mitya Jul 12 '12 at 16:11
  • If I know an example how to put it to work in context of opera ext. The thing is web workers are for downloading files from server, and with extension I must access local files. – rsk82 Jul 12 '12 at 16:21
  • Just floating it as an idea - not certain it would be suitable for you, but I think web workers are OK with local files, not sure. [See this answer](http://stackoverflow.com/questions/11291637/daemon-thread-in-javascript/11292100#11292100) for a simple web workers demo you might want to try. Sorry I couldn't be more help. – Mitya Jul 12 '12 at 16:24
  • Have you tried using opera.postError() to post messages to the console instead of alert() ? Any errors in your error console in the first place? – hallvors Jul 13 '12 at 07:59
  • No, no errors. I checked that many times. And that's why I used apod site, there is no JS there so I won't be overwhelmed by external errors. – rsk82 Jul 13 '12 at 09:56
  • 1
    Try to enable opera:config#Always%20Load%20User%20Javascript . Does that help? – hallvors Jul 24 '12 at 02:52

1 Answers1

2

The opera.extension.postMessage() method cannot be called from the background process because it doesn't know which page/tab to post the message to. Instead, opera.extension.broadcastMessage() can be used to send a message to all tabs (see: http://dev.opera.com/articles/view/extensions-api-messaging-broadcastmessage/ )

Alternatively, to send a message to the injected script in a particular tab:

  1. Listen for a connection event (opera.extension.onconnect) in the background process
  2. Run postMessage() from the event's source object

See: http://dev.opera.com/articles/view/opera-extensions-messaging/#backgroundscript_injectedscript

By the way, don't forget that what you use cross-domain XHR in the background process, you'll need to allow access to the target domain in your config.xml file, for example:

<access origin="http://apod.nasa.gov" subdomains="true"/>
tagawa
  • 4,561
  • 2
  • 27
  • 34