我正在开发Safari App Extension(因为Safari Extensions现已正式弃用),我想通过JS将一些HTML注入页面。但是,当我向safari-extension://
URL发出请求时,请求是在没有SSL的情况下发出的,并且Safari当前会阻止混合内容,并且不允许任何方式更改该策略。所以我有两个问题。
如何解决我的开发环境中的这个问题?
我在评论here中读到生产打包扩展程序(旧的Safari扩展程序)将使用SSL加载资源。这适用于Safari App Extensions吗?
修改
我有一个Apple开发者帐户,签了我的分机,但仍然没有运气。
答案 0 :(得分:1)
我使用Safari应用程序扩展程序公开的messaging protocol野生动物园来在响应中将HTML作为字符串发送。
let sendMessage = (msgObj, callback) => {
msgObj.callbackIndex = (safariCallbackCount++).toString();
let callbackKey = msgObj['type']+'_'+msgObj.callbackIndex;
if (typeof callback === 'function')
messageCallbacks[callbackKey] = callback;
safari.extension.dispatchMessage(msgObj['type'], msgObj);
}
sendMessage({type: 'loadTemplate', path: path}, callback );
请注意 messageCallbacks 哈希,该哈希存储回调函数以及用于索引唯一性的计数器。您正在将消息传递给Swift进程,发送多条消息时,响应可能会混乱。
func loadTemplate (withPage page: SFSafariPage, withUrl url: URL, withCallbackIndex callbackIndex: String) {
let pathExtention = url.pathExtension
let pathPrefix = url.path.replacingOccurrences(of: "." + pathExtention, with: "", options: .literal, range: nil)
if let filepath = Bundle.main.path(forResource: pathPrefix, ofType: pathExtention) {
do {
let contents = try String(contentsOfFile: filepath, encoding: .utf8)
page.dispatchMessageToScript(withName: "loadTemplate", userInfo: ["htmlData": contents, "callbackIndex": callbackIndex])
} catch {
// contents could not be loaded
NSLog("ERROR CONTENTS COULD NOT BE LOADED FROM \(filepath)")
}
} else {
NSLog("Could not build file path")
}
}
// This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
page.getPropertiesWithCompletionHandler { properties in
switch messageName {
case "loadTemplate":
self.loadTemplate(withPage: page, withUrl: URL(string: userInfo?["path"] as! String)!, withCallbackIndex: userInfo?["callbackIndex"] as! String)
default:
NSLog("NO DEFINITION FOR STRING VALUE")
}
}
}
let handleMessage = event => {
let callbackIndex = event.message.callbackIndex;
delete event.message.callbackIndex;
let callbackKey = event.name+'_'+callbackIndex;
if ( typeof messageCallbacks[callbackKey] === 'function' ) {
messageCallbacks[callbackKey](event.message);
delete messageCallbacks[callbackKey]
}
};
safari.self.addEventListener("message", handleMessage);
//// html data is accessible from event.message.htmlData