Is it possible to import content scripts out of project folder for Safari App Extension?

530 views Asked by At

I'm developing a Safari App Extension. But my content scripts are out of Xcode project folder. Is it possible to import those script files?

I see in Apple's official document:

For each script file, add a dictionary to the array. Each dictionary must have a Script key whose value specifies the path (relative to the Resources directory of the bundle) to the script file to include.

Assume my extension project called SafariExt. Folder structure of my app:

SafariExt   // Xcode project root folder
 |
 +-- SafariExt Extension
    |
    +--script.js    // this is the default script file in Xcode project, and I specified path relative to this folder


Scripts     // my scripts folder
 |
 +-- myScript.js    // the file I want to import

SafariExt and Scripts are in the same base folder.

Now I'm using ../../Scripts/myScript.js in Info.plist to refer to myScript.js file, but in Safari this file doesn't get loaded.

Info.plist looks like below:

<key>SFSafariContentScript</key>
<array>
    <dict>
        <key>Script</key>
        <string>../../Scripts/myScript.js</string>
    </dict>
</array>
1

There are 1 answers

1
Kevin Ghadyani On

You should be able to load content scripts from the web using safari.addContentScriptFromURL. You could try using that in conjunction with file:// to see if it allows you to pull from outside your project directory.

Really though, you should be copying files into your extension directory with a build script if possible instead of having relative directory links.

https://developer.apple.com/documentation/safariextensions/safariextension/1635361-addcontentscriptfromurl