coordinates
10/7/2014 - 3:13 AM

Detect OGP tags

Detect OGP tags

// https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW12

var MyExtensionJavaScriptClass = function() {};

MyExtensionJavaScriptClass.prototype = {
    run: function(arguments) {
        // selection string
        var selectionString = window.getSelection().toString();
        
        // open graph
        var ogMetas = {};
        var metatags = document.getElementsByTagName("meta");

        if(0 < metatags.length) {
            for (i = 0; i < metatags.length; i++) {
                var property = metatags[i].getAttribute("property");
                var content = metatags[i].getAttribute("content");

                if( "og:title" == property) {
                    ogMetas.title = content;
                }
                else if( "og:description" == property) {
                    ogMetas.description = content;
                }
                else if( "og:site_name" == property) {
                    ogMetas.site_name = content;
                }
                else if( "og:url" == property) {
                    ogMetas.url = content;
                }
                else if( "og:type" == property) {
                    ogMetas.type = content;
                }
                else if ( "og:image" == property ||
                  "og:image:url" == property ||
                  "og:image:secure_url" == property ){
                    var image = {"url": content};

                    for (var j=i+1; j<metatags.length; j++) {
                        var imageProperty = metatags[j].getAttribute("property");
                        var imageContent = metatags[j].getAttribute("content");

                        if("og:image:width" == property) {
                            image.width = imageContent;
                            i = j;
                        }
                        else if("og:image:height" == property) {
                            image.height = imageContent;
                            i = j;
                        }

                        if(3 <= image.length) {
                            break;
                        }
                    }

                    if(undefined === ogMetas.image) {
                        ogMetas.image = [image];
                    }
                    else {
                        ogMetas.image[ogMetas.image.length] = image;
                    }
                }

            }
        }

        arguments.completionFunction({
                                     "baseURI": document.baseURI,
                                     "selectionString": selectionString,
                                     "ogMetas": ogMetas,
                                     });
    },

    // 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();