我正在开发一个 Safari 应用程序扩展(因为 Safari 扩展现在已正式弃用(,我想通过 JS 将一些 HTML 注入页面。但是,当我向我的safari-extension://
URL 发出请求时,请求是在没有 SSL 的情况下发出的,并且 Safari 目前阻止混合内容并且不允许以任何方式更改该策略。所以我有两个问题。
-
如何为我的开发环境解决此问题?
-
我在这里的评论中读到,生产打包扩展(旧的 Safari 扩展(将使用 SSL 加载资源。Safari 应用扩展程序是这样吗?
编辑
我有一个Apple开发人员帐户,签署了我的扩展程序,但仍然没有运气。
我使用Safari向Safari应用程序扩展公开的消息传递协议在响应中将HTML作为字符串发送。
内容脚本请求 HTML 模板 (JS(
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 进程,并且在发送多条消息时,响应可能会不按顺序返回。
SFSafariExtensionHandler 侦听消息和句柄 (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")
}
}
}
内容脚本处理响应 (JS(
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