How to handle javascript asynchronous completion in iOS sharing extension?

I am making a simple iOS extension extension that will work on web pages. This is not like Pinterest, as it captures certain information from the page. But it differs from Pinterest in that the JS used to capture is sometimes common in the domain of a webpage.

So, in the run () function of ExtensionPreprocessingJS, I do the following to load and execute 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 information from the page to β€œpin”.

My problem is that this happens asynchronously and not before I return the results at the end of the run method using

 arguments.completionFunction( <my JSON here> ); 

I tried to wait for the result to be set before calling the completion () function, but the extension seems to refer to this as completion function Function () is not called at all. That is, apparently, asynchronous JS is not provided in the design of the share extension framework. It assumes that the JS result is available after the run () function returns.

Am I misunderstanding the design of the share extension, or do I need to look for another way to do this? I notice that Instapaper created a sharing extension without its own user interface, and I wonder why they did it.

+5
source share

Source: https://habr.com/ru/post/1245862/


All Articles