UNPKG

@oothkoo/seo-js

Version:

A simple vanilla JavaScript SEO library for SPAs (Single Page Applications).

3 lines 3.44 kB
/* Seo.js v1.0.5 by Tierry Danquin https://github.com/oOthkOo/seo.js */ function Seo(e){this.options=Object.assign(this.getDefaultOptions(),e),this.clearMetas(),this.clearTerms()}Seo.prototype.version="1.0.5",Seo.prototype.getDefaultOptions=function(){return{debug:!1,headSelector:"head",ignoreEmptyTagValue:!0}},Seo.prototype.use=function(e){Array.isArray(e)?e.forEach(function(e){this.metas[e.name]=e}.bind(this)):"object"==typeof e&&(this.metas[e.name]=e)},Seo.prototype.getMessageText=function(e){return"[Seo.js] - "+e+"."},Seo.prototype.log=function(e){console.log(this.getMessageText(e))},Seo.prototype.clearMetas=function(){this.metas={}},Seo.prototype.getMetaTypeTag=function(e){return{name:e,selector:e,template:"<"+e+">{value}</"+e+">"}},Seo.prototype.tag=function(e){return Array.isArray(e)?e.map(function(e){return this.getMetaTypeTag(e)}.bind(this)):"string"==typeof e?this.getMetaTypeTag(e):void 0},Seo.prototype.getMetaTypeName=function(e){return{name:e,selector:'meta[name="'+e+'"]',template:'<meta name="'+e+'" content="{value}" />'}},Seo.prototype.metaName=function(e){return Array.isArray(e)?e.map(function(e){return this.getMetaTypeName(e)}.bind(this)):"string"==typeof e?this.getMetaTypeName(e):void 0},Seo.prototype.getMetaTypeProperty=function(e){return{name:e,selector:'meta[property="'+e+'"]',template:'<meta property="'+e+'" content="{value}" />'}},Seo.prototype.metaProperty=function(e){return Array.isArray(e)?e.map(function(e){return this.getMetaTypeProperty(e)}.bind(this)):"string"==typeof e?this.getMetaTypeProperty(e):void 0},Seo.prototype.getMetaTypeLink=function(e){return{name:e,selector:'link[rel="'+e+'"]',template:'<link rel="'+e+'" href="{value}" />'}},Seo.prototype.link=function(e){return Array.isArray(e)?e.map(function(e){return this.getMetaTypeLink(e)}.bind(this)):"string"==typeof e?this.getMetaTypeLink(e):void 0},Seo.prototype.throwError=function(e){throw new Error(this.getMessageText(e))},Seo.prototype.domRemove=function(e){this.domFind(this.options.headSelector).removeChild(e)},Seo.prototype.domFind=function(e){return document.querySelector(e)},Seo.prototype.domCreate=function(e){var t=document.implementation.createHTMLDocument();return t.body.innerHTML=e,t.body.firstChild},Seo.prototype.domAppend=function(e,t){return e.appendChild(t)},Seo.prototype.clear=function(){Object.keys(this.metas).forEach(function(e){var t=this.metas[e],o=this.domFind(t.selector);o&&this.domRemove(o)}.bind(this))},Seo.prototype.update=function(a,e){var t={};if(e)t=a;else{var o={};Object.keys(this.metas).forEach(function(e){o[e]=""}),t=Object.assign(o,a)}Object.keys(t).forEach(function(e){var t=this.metas[e];if(t){var o=this.options.headSelector,r=this.domFind(o);r||this.throwError("head ("+o+") element not found");var n=a[e]||null;if(n||!this.options.ignoreEmptyTagValue){Object.keys(this.terms).forEach(function(e){var t=new RegExp("{"+e+"}","gi");n=n.replace(t,this.terms[e])}.bind(this));var i=t.template.replace(/\{value\}/g,n);this.options.debug&&this.log(i);var s=this.domFind(t.selector);s&&(this.domRemove(s),this.options.debug&&this.log("meta ("+e+") removed")),this.domAppend(r,this.domCreate(i))}this.options.debug&&this.log("meta name["+e+"] value["+n+"]")}else this.throwError("meta ("+e+") not registered")}.bind(this))},Seo.prototype.clearTerms=function(){this.terms={}},Seo.prototype.updateTerms=function(e){this.terms=Object.assign(this.terms,e)},module&&(module.exports=Seo);