-
Notifications
You must be signed in to change notification settings - Fork 3
/
schemaFilter.min.js
1 lines (1 loc) · 1.97 KB
/
schemaFilter.min.js
1
var SchemaFilter={schemaClass:new Set,onLoad:"load",remove:function(e,t,r){switch(null==e?this.schemaClass=new Set:(e.constructor===Array&&e.length>=0&&(this.schemaClass=new Set(e)),e.constructor===Set&&(this.schemaClass=e)),null==t&&(t="micro"),t){case"microdata":case"micro":case"rdfa":SchemaFilter.removeMicroData();break;case"json-ld":SchemaFilter.removeJSONLD(r)}switch(t){case"microdata":case"micro":case"rdfa":window.addEventListener(this.onLoad,(function(e){SchemaFilter.removeMicroData()}));break;case"json-ld":window.addEventListener(this.onLoad,(function(e){SchemaFilter.removeJSONLD(r)}))}},removeMicroData:function(){var e=document.querySelectorAll("[itemtype]");0===e.length&&(e=document.querySelector("html"));var t=[];if(0!==this.schemaClass.size)for(var r=0;r<e.length;r++){var o=e[r],a=o.getAttribute("itemtype").split("/").pop();SchemaFilter.schemaClass.has(a)&&t.push(o)}else t=e;for(r=0;r<t.length;r++){var s=t[r];s.removeAttribute("itemtype");for(var i=s.querySelectorAll("[itemprop]"),c=0;c<i.length;c++){var n=i[c];n.removeAttribute("itemprop"),n.removeAttribute("itemscope"),n.removeAttribute("content")}}},removeJSONLD:function(e){this.onlyRemoveJsonWithNoAtID=!!e;for(var t=document.querySelectorAll('[type="application/ld+json"]'),r=[],o=0;o<t.length;o++){try{var a=JSON.parse(t[o].innerHTML);if(this.onlyRemoveJsonWithNoAtID){var s=a;Array.isArray(a)||(s=[a]);for(var i=!0,c=0;c<s.length;c++)if(s[c]["@id"]){i=!1;break}i&&t[o].remove()}else if(0===this.schemaClass.size){t[o].remove();continue}}catch(e){continue}r.push([t[o],a])}for(o=0;o<r.length;o++){var n=r[o],l=n[0],h=n[1];this.schemaClass.has(h["@type"])&&l.remove()}},removeRDFa:function(){var e=document.querySelectorAll("[typeof]"),t=[];if(0!==this.schemaClass.size)for(var r=0;r<e.length;r++){var o=e[r];this.schemaClass.has(o.getAttribute("typeof"))&&t.push(o)}else t=e;t.forEach((function(e){for(var t=e.querySelectorAll("[property]"),r=0;r<t.length;r++)t[r].removeAttribute("property"),t[r].removeAttribute("typeof")}))}};