I am trying to use an otf file that the user selects from their computer in an injected script from my Safari App Extension (macOS), but currently, I can not get Safari to load the file due to permission issues.
This is what I am currently doing:
First I allow the user to select a file (code below):
var newFileURL = "";
let openPanel = NSOpenPanel()
openPanel.allowedFileTypes = ["otf"]
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canChooseFiles = true
openPanel.runModal()
newFileURL = openPanel.url?.absoluteString ?? ""
NSLog(newFileURL)
Next I send the variable ‘newFileUrl’ as a message to the default script.js and I handle the message with this javascript code:
function handleMessage(event) {
var css = "@font-face {font-family: userFont; src: url('file:///" + event.name + "');}",
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
console.log(event.name);
head.appendChild(style);
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
When I do this I get the an error in the safari developer console, “Not allowed to load local resource: file:///Users/macuser/Downloads/file.otf”
I have allowed read/write permissions for user files for my extension so I am not sure how I would go about allowing user files for the injected script.
Thanks!