Commit 4efadd8b authored by Peter Kalchgruber's avatar Peter Kalchgruber

init

parents
function getJSONdata(){
/*
parse all JSON-LD data of current page
*/
result = [];
rawdata = ($('script[type="application/ld+json"'));
$.each(rawdata, function(){
result.push(jQuery.parseJSON(rawdata.html()));
});
return result;
}
function formatObject(value){
/*
if the passed object is an object it will be stringified and returned otherwise value will be returned raw
*/
if (typeof(value) =="object"){
strobj = JSON.stringify(value, null, '\t').replace(/\n/g,'<br />').replace(/\t/g,'&nbsp;&nbsp;&nbsp;');
strobj = JSON.stringify(value, null, 2);
strobj = strobj.substr(1,strobj.length-2);
return '<pre class="object">' + strobj + '</pre>';
}
return value;
}
function start(){
jsondata = getJSONdata();
microdata = converter.convert();
if (microdata !== null && jsondata.length !== 0){
console.log(1);
$.each(jsondata, function(){
//if (this['@context'] == microdata['@context']){ /normalize url missing TODO
if ('@graph' in microdata){
microdata['@graph'].push(this);
}else{
newmd = {};
newmd['@context'] = microdata['@context'];
newmd['@graph'] = [];
delete microdata['@context'];
newmd['@graph'].push (microdata);
newmd['@graph'].push(this);
microdata = newmd;
}
//}
});
data = microdata;
format = "jsonld+microdata";
}else if(microdata === null){
if (jsondata.length > 1){
newmd = {};
newmd['@context'] = jsondata['@context'];
newmd['@graph'] = [];
delete jsondata['@context'];
newmd['@graph'] = (jsondata);
data = newmd;
}else{
data = jsondata[0];
}
format = "jsonld";
}else if(jsondata.length === 0){
data = microdata;
format = "microdata";
}
// else{
// format += "+microdata";
// data += converter.convert();
// }
if (data != null){
console.dir(data);
type = data['@type'];
name = data['name'];
num = Object.keys(data).length
div = $('<div id="infodiv" style="opacity: 0.9; z-index:10000000000;width:90%;background-color:white;border:1px solid;padding:15px;position:absolute;top:5px;"></h1>');
ul = $('<ul>');
if (Array.isArray(data) && '@graph' in data){
name = "Graph";
num = "graph entries: "+num;
}
ul.append("<li>@type: " + type + "</li>");
ul.append("<li>name: " + name + "</li>");
ul.append("<li>format: " + format + "</li>");
ul.append("<li>#: " + num + "</li>");
div.append(ul);
b2 = '<button type="button" class="more btn btn-default">more</button>';
xbutton = $('<button type="button" class="btn btn-default" id="closebtn" style="position:absolute;right:2px;top:2px;height:40px;width:40px;font-size:20px;font-weight:bold">x</button>');
div.append(xbutton);
datadiv = $('<div style="display:none" id="datadiv">'+formatObject(data)+'</div>');
div.append(datadiv);
div.append(b2);
$(document).on("dblclick","#infodiv",function(){
$(this).hide();
});
$(document).on("click","#closebtn",function(){
$(this).parent().hide();
});
$(document).on("click",".more",function(){
$('#datadiv').show();
$(this).html("less");
$(this).addClass("less");
$(this).removeClass("more");
});
$(document).on("click",".less",function(){
$('#datadiv').hide();
$(this).html("less");
$(this).addClass("more");
$(this).removeClass("less");
});
$('body').prepend(div);
}
else{
console.log("no struc data found");
}
}
start();
\ No newline at end of file
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
icon.png

17.6 KB

This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
/* globals jQuery:false
*
* jQuery Microdata v1.4
* https://github.com/hubgit/jquery-microdata
*
* Copyright 2014 Alf Eaton
* Released under the MIT license
* http://git.macropus.org/mit-license/
*
* Date: 2014-01-21
*/
(function($) {
'use strict';
// get all items of a certain type
$.fn.items = function(itemtype) {
return this.find('[itemscope]:not([itemprop])').filter(function() {
return !itemtype || attrs.call(this, 'itemtype').get().indexOf(itemtype) !== -1;
});
};
// all nodes with a certain property name
$.fn.property = function(name) {
return propertyNodes.apply(this).filter(function() {
return attrs.call(this, 'itemprop').get().indexOf(name) !== -1;
});
};
// get/set the value of matched elements
$.fn.value = function(value) {
// get value
if (typeof value === 'undefined') {
return itemValue.call(this);
}
// set a single value
itemValue.call(this, value);
};
// get all values of a property as an array
$.fn.values = function(expanded) {
return this.map(function() {
var node = $(this);
return node.is('[itemscope]') ? microdata.call(node, expanded) : itemValue.call(node);
}).get();
};
// convenience function for manipulating properties of an item
$.fn.microdata = function(value) {
switch (typeof value) {
// get all values of an item
case 'undefined':
case 'boolean':
return this.map(function() {
return microdata.call($(this), value);
}).get();
// get/set a single property
case 'string':
if (arguments.length === 1) {
// get a single property
return this.property(value).value();
}
// set a single property
this.property(value).value(arguments[1]);
return this;
// set the value of multiple properties
case 'object':
var nodes = this;
$.each(value, function(name, value) {
nodes.property(name).value(value);
});
return this;
}
};
var ownerElement = function(node) {
var owner = node;
while (owner.parentNode && owner.parentNode.nodeType === 1) {
owner = owner.parentNode;
}
return $(owner);
};
// all property nodes, including those in referenced nodes
var propertyNodes = function() {
if (!this.length) {
return $([]);
}
var owner = ownerElement(this.get(0));
var refs = attrs.call(this, 'itemref').map(function(i, id) {
return owner.find('#' + id).get(0);
});
var nodes = $.merge($(refs), this);
return nodes.find('[itemprop]').not(nodes.find('[itemscope] [itemprop]'));
};
// get a space-separated attribute as an array
var attrs = function(attribute) {
return $(this).map(function() {
return (this.getAttribute(attribute) || '').split(/\s+/);
}).filter(function() {
return this.length;
});
};
// get or set the value of a node
var itemValue = function(value) {
var getting = value == null;
if (this.is('[itemscope]')) {
if (!getting) {
throw 'Not allowed to set the value of an itemscope node';
}
return this;
}
var node = this.get(0);
switch (node.nodeName) {
case 'META':
return getting ? $.trim(this.attr('content')) : this.attr('content', value);
case 'DATA':
return getting ? $.trim(this.attr('value')) : this.attr('value', value);
case 'METER':
return getting ? $.trim(this.attr('value')) : this.attr('value', value);
case 'TIME':
if (getting) {
if (this.attr('datetime')) {
return $.trim(this.attr('datetime'));
}
return $.trim(this.text());
}
return this.attr('datetime', value);
case 'AUDIO':
case 'EMBED':
case 'IFRAME':
case 'IMG':
case 'SOURCE':
case 'TRACK':
return getting ? node.src : this.attr('src', value);
case 'A':
case 'AREA':
case 'LINK':
return getting ? node.href : this.attr('href', value);
case 'OBJECT':
return getting ? node.data : this.attr('data', value);
default:
return getting ? $.trim(this.text()) : this.text(value);
}
};
// get all properties as a key/value(s) object
var microdata = function(expanded) {
if (this.length > 1) {
return this.map(function() {
return microdata.call($(this), expanded);
}).get();
}
// the object always includes an itemtype
var data = {
type: expanded ? attrs.call(this, 'itemtype').get() : attrs.call(this, 'itemtype').get(0)
};
propertyNodes.call(this).map(function() {
var node = $(this);
var property = node.value();
if (property instanceof jQuery) {
property = microdata.call(property, expanded);
}
attrs.call(this, 'itemprop').each(function(index, name) {
if (expanded) {
if (typeof data[name] == 'undefined') {
data[name] = [];
}
data[name].push(property);
} else {
if (typeof data[name] == 'undefined') {
data[name] = property; // first item
} else if ($.isArray(data[name])) {
data[name].push(property); // more items
} else {
data[name] = [data[name], property];
}
}
});
});
return data;
};
}(jQuery));
/**
* A JavaScript implementation of Microdata to JSON-LD Converter
* @author Peter Kalchgruber
*
* Copyright 2017, All rights reserved.
*/
(function(window){
'use strict';
function convert(){
var _converterObject = {};
/**
* Remove Spaces, Linebreaks...
* @param {String} val
* @return {String} val
*/
function _clean(val){
if(typeof val == 'undefined' || typeof val === 'object')
return val;
return (val.replace(/(\r\n|\n|\r})/gm,'').replace(/\s\s+/g,' ')).trim();
}
/**
* Retrieve values of HTML element
* @param {DOM object} el
* @return {String} value
*/
function _getval(el){
return _clean($(el).attr('href') || $(el).prop('content') || $(el).text() || $(el).attr('src'));
}
/**
* Append value to json-ld object. If property is already assigned, create array and return object
* @param {object} el
* @param {String} key
* @param {String} val
* @return {object} el
*/
function _append(el,key,val){
if(key in el && Array.isArray(el[key])){
el[key].push(val);
}else if(key in el){
el[key] = jQuery.makeArray(el[key]);
el[key].push(val);
}else{
el[key] = val;
}
return el;
}
/**
* Convert Microdata element to JSON-LD object
* @param {object} topel
* @return {object} im
*/
function _get(topelem){
var im={};
im['@type'] = $(topelem).attr('itemtype');
$(topelem).find('[itemprop]').attr('itemprop', function(i, key) {
if ($(this).parents('[itemscope]').get(0) == topelem){
if ($(this).is('[itemscope]') && $(this).is('[itemprop]')) {
var subitem = _get(this);
im = _append(im,$(this).attr('itemprop'),subitem);
}else{
var val = _getval(this);
im = _append(im,key,val);
}
}
});
return im;
}
/**
* Convert all microdata elements in json-ld format.
* Need to filter to only convert top-itemscope elements and further to only process
* itemprops that are direct child of current itemscope element and not process values
* that contain other itemprop elements
* @return {Object} item
*/
_converterObject.convert = function(){
var item = [];
$('[itemscope]').map(function(i, el){
if($(el).parents('[itemscope]').length == 0){ //only proceed with top itemscope entries
item.push(_get(el));
}
});
if(item.length>1){
var jsonld = {};
jsonld['@context'] = 'http://schema.org/';
jsonld['@graph'] = item
return jsonld;
}else if(item.length==1){
item[0]['@context'] = 'http://schema.org/';
return item[0];
}else{
return null;
}
}
return _converterObject;
}
if(typeof(window.converter) === 'undefined'){
window.converter = convert();
}
})(window); // We send the window variable withing our function
{
"manifest_version": 2,
"name": "Finder Addon",
"description": "This extension finds structured data in the open website",
"version": "0.0.1",
"page_action": {
"default_icon": "icon.png"
},
"permissions" : [
"tabs", "http://*/*", "https://*/*", "storage"
],
"content_scripts": [
{
"css": ["css/style.css"],
"matches": ["file:///*/*", "http://*/*", "https://*/*"],
"js": ["js/jquery-1.10.2.min.js", "js/jquery-ui-1.10.3.custom.min.js", "js/jquery.microdata.js", "js/microdata-to-jsonld.js", "content.js"]
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment