I am making a simple iOS share extension that will operate on web pages. It is not unlike Pinterest, in that it grabs certain info from the page. But it is different from Pinterest in that the JS used to do the grabbing is sometimes custom to the web page domain.
So in the ExtensionPreprocessingJS run() function I do the following to load and execute the custom JS:
var Action = function() {};
Action.prototype = {
run: function(arguments) {
var e = document.createElement('script');
e.setAttribute('type','application/javascript');
e.setAttribute('id','my-unique-id');
e.setAttribute('src','//mydomain.com/bookmarklet?d='+document.domain);
document.body.appendChild(e);
<some code to extract results and generate JSON>
arguments.completionFunction({ <my JSON here> });
}
};
var ExtensionPreprocessingJS = new Action
My bookmarklet endpoint provides some JS that finds the relevant info from the page for "pinning".
My problem is that this happens asynchronously and not before I pass back the results at the end of the run method using
arguments.completionFunction( <my JSON here> );
I have tried waiting for the result to be set before calling completionFunction() but the extension seems to treat that as completionFunction() not being called at all. That is, it seems the design of the share extension framework doesn't accommodate asynchronous JS. It assumes the result from the JS is available after the run() function returns.
Am I misunderstanding the share extension design, or do I need to look for a different way to do this? I do notice that Instapaper created a share extension with no native UI and I'm wondering whether this is why they did so.