javascript - Access webpage properties in Share extension - Stack Overflow

I want to access webpage properties (Title, Meta - description, URL, default image, etc) when user open

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 (.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.
        argumentspletionFunction({"url": document.baseURI});
        argumentspletionFunction({"host": getHost()});
        argumentspletionFunction({"title": document.title});
        argumentspletionFunction({"description": getDescription()});
        argumentspletionFunction({"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 pleteRequestReturningItems:pletion:].
    // 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.

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./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.pletionFunction({"url": document.baseURI});
        arguments.pletionFunction({"host": getHost()});
        arguments.pletionFunction({"title": document.title});
        arguments.pletionFunction({"description": getDescription()});
        arguments.pletionFunction({"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 pleteRequestReturningItems:pletion:].
    // 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.

Share Improve this question asked Oct 19, 2015 at 1:14 PurusottamPurusottam 6932 gold badges6 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Here is how i solved this problem.

JS Code:

var MyExtensionJavaScriptClass = function() {};

MyExtensionJavaScriptClass.prototype = {
    getDescription: function() {
        var metas = document.getElementsByTagName('meta'); 
        for (i=0; i<metas.length; i++) { 
            if (metas[i].getAttribute("name") == "description") { 
                return metas[i].getAttribute("content"); 
            }
        }
        return "";
    },
    getImage: function() {
        var metas = document.getElementsByTagName('meta'); 
        for (i=0; i<metas.length; i++) { 
            if (metas[i].getAttribute("name") == "og:image" || metas[i].getAttribute("name") == "sailthru.image.full" || metas[i].getAttribute("name") == "twitter:image:src") { 
                return metas[i].getAttribute("content"); 
            }
        }
        return "";
    },
    run: function(arguments) {
    // Pass the baseURI of the webpage to the extension.
        arguments.pletionFunction({"url": document.baseURI, "host": document.location.hostname, "title": document.title, "description": this.getDescription(), "image": this.getImage()});
    },
// Note that the finalize function is only available in iOS.
    finalize: function(arguments) {
        // arguments contains the value the extension provides in [NSExtensionContext pleteRequestReturningItems:pletion:].
    // 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;

// ExtensionPreprocessingJS.test();

Swift Code:

for item: AnyObject in (self.extensionContext?.inputItems)! {
    let inputItem = item as! NSExtensionItem

    for provider: AnyObject in inputItem.attachments! {
        let itemProvider = provider as! NSItemProvider

        if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) {
            itemProvider.loadItemForTypeIdentifier(kUTTypePropertyList as String, options: nil, pletionHandler: { (result: NSSecureCoding?, error: NSError!) -> Void in
                if let resultDict = result as? NSDictionary {
                    self.articleTitle = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["title"] as! String
                    self.articleHost = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["host"] as! String
                    self.articleDesc = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["description"] as! String
                    self.articleImage = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["image"] as! String
                    self.articleUrl = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["url"] as! String
                }
            })
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742325394a4422646.html

相关推荐

  • javascript - Access webpage properties in Share extension - Stack Overflow

    I want to access webpage properties (Title, Meta - description, URL, default image, etc) when user open

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信