MaxAst / expo-share-extension

Expo config plugin for creating iOS share extensions with a custom view.
MIT License
205 stars 4 forks source link

Custom preprocessing #12

Closed MaxAst closed 7 months ago

MaxAst commented 7 months ago

What's new

As explained in #11, we can use a JavaScript file to preprocess the webpage before the share extension is activated (See Apple Docs). This is useful if you want to extract the title and URL of the webpage, for example. So far, expo-share-extension used a hard-coded preprocessing.js file to extract some basic data. With this PR, users of this plugin can now provide their own preprocessing file by changing their app.json/app.config.(j|t)s like so:

[
  "expo-share-extension",
    {
      "preprocessingFile": "./preprocessing.js"
    },
],

The preprocessingFile option adds NSExtensionActivationSupportsWebPageWithMaxCount: 1 as an NSExtensionActivationRule. Your preprocessing file must adhere to some rules:

  1. You must create a class with a run method, which receives an object with a completionFunction method as its argument. This completionFunction method must be invoked at the end of your run method. The argument you pass to it, is what you will receive as the preprocessingResults object as part of initial props.
class ShareExtensionPreprocessor {
  run(args) {
    args.completionFunction({
      title: document.title,
    });
  }
}
  1. Your file must create an instance of a class using var, so that it is globally accessible.
var ExtensionPreprocessingJS = new ShareExtensionPreprocessor();

For a full example, check this.

Warning

Using this option enbales NSExtensionActivationSupportsWebPageWithMaxCount: 1 and this is mutually exclusive with NSExtensionActivationSupportsWebURLWithMaxCount: 1, which expo-share-extension enables by default. This means that once you set the preprocessingFile option, you will no longer receive url as part of initial props. However, you can still get the URL via preprocessingResults by using window.location.href in your preprocessing file: