我希望能够分享选定的文本,但我的扩展程序只在单击共享图标时出现(然后它会用页面标题填充字段)。我希望我的扩展程序出现在用户选择文本并单击“分享…”(如下图所示)时,并希望它将选定的文本填充到文本区域中。
共享视图控制器:override func viewDidLoad() {
super.viewDidLoad()
customPopup()
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary {
print("RESULTS: \n", results)
}
}
})
} else {
print("error")
}
}
Action.js(JS 预处理)
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;
Info.plist