1
当用户使用JavaScript文件在iOS上打开共享扩展时,我想访问网页属性(标题,元描述,URL,默认图像等)。我使用的JavaScript(https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW12)下面的代码:访问共享扩展中的网页属性
var MyExtensionJavaScriptClass = function() {};
MyExtensionJavaScriptClass.prototype = {
run: function(arguments) {
// Pass the baseURI of the webpage to the extension.
arguments.completionFunction({"url": document.baseURI});
arguments.completionFunction({"host": getHost()});
arguments.completionFunction({"title": document.title});
arguments.completionFunction({"description": getDescription()});
arguments.completionFunction({"image": getImage()});
},
getHost: function() {
var l = document.createElement("a");
l.href = href;
return l.hostname;
},
getDescription: function() {
var metas = document.getElementsByTagName('meta');
for (i=0; i<metas.length; i++) {
if (metas[i].getAttribute("property") == "description") {
return metas[i].getAttribute("content");
}
}
return "";
},
getImage: function() {
// Need to find this out
return "";
},
// Note that the finalize function is only available in iOS.
finalize: function(arguments) {
// arguments contains the value the extension provides in [NSExtensionContext completeRequestReturningItems:completion:].
// In this example, the extension provides a color as a returning item.
document.body.style.backgroundColor = arguments["bgColor"];
}
};
// The JavaScript file must contain a global object named "ExtensionPreprocessingJS".
var ExtensionPreprocessingJS = new MyExtensionJavaScriptClass;
这是访问的网页特性也什么是在内容获取第一图像的最佳方法的正确途径。
任何帮助将不胜感激。
你的JS是不是对图像加工.. –
我的意思是METAS [I] .getAttribute( “名”)==“OG :图像“不是返回图像。 –
@AsadullahAli该条件的目的是检查URL中是否包含开放图形标签或sailthru或twitter卡。如果标签存在,则获取图像。此代码可能需要根据您正在测试的URL进行一些更新。你能分享你正在测试的网址吗? – Puru