I want to access webpage properties (Title, Meta - description, URL, default image, etc) when user opens Share extension on iOS using javascript file. I am using the following code for 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;
Is this the right way to access Web page properties also what's the best way to fetch the first image in the content.
Any help would be much appreciated.