UNPKG

cp-metas

Version:

set website title

2 lines (1 loc) 3.58 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vue")):"function"==typeof define&&define.amd?define(["exports","vue"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).cpMeta={},t.vue)}(this,(function(t,e){"use strict";const n="undefined"!=typeof window;function a(t){return t=(t=(t=(t=(t=t.replace(/&/g,"&amp;")).replace(/</g,"&lt;")).replace(/>/g,"&gt;")).replace(/"/g,"&quot;")).replace(/'/g,"&#039;")}class i{#t;#e=!1;head;defaultTitle;constructor(t,e,n){this.head=t,this.#t=e,this.defaultTitle=t.title,this.#e=n}setHead(t={}){t.title&&(this.head.title=t.title||this.defaultTitle);const e=[];for(const n in t.meta)if(t.meta.hasOwnProperty(n)){const a=t.meta[n];switch(a.name||a.property){case"description":this.head.description=a.content;break;case"keywords":this.head.keywords=a.content;break;case"og:image":this.head.image=a.content;break;default:e.push(a)}}if(this.head.metas=e,!this.#t){const e=document.getElementsByTagName("title");if(e&&e.length)e[0].innerText=t.title||this.defaultTitle;else{const e=document.createElement("title");e.innerText=t.title||this.defaultTitle,document.getElementsByTagName("head")[0].appendChild(e)}if(n&&this.#e){const t=document.querySelectorAll('meta[data-mark="cp"]');for(let e in t)if(t.hasOwnProperty(e)){t[e].remove()}for(let t in this.head.metas){const e=this.head.metas[t];let n="",a="",i="";if(e.name?(n="name",a=e.name):e.property&&(n="property",a=e.property),!n)return;e.content&&(i=e.content);const o=document.createElement("meta");o.setAttribute(n,a),o.setAttribute("content",i),o.setAttribute("data-mark","cp"),document.getElementsByTagName("head")[0].appendChild(o)}}}}getHead(){let t=this.head.description,e=this.head.keywords,n=this.head.description,i=this.head.image;const o=[];for(const s in this.head.metas)if(this.head.metas.hasOwnProperty(s)){const r=this.head.metas[parseInt(s)];switch(r.name||r.property){case"description":t=a(r.content);break;case"keywords":if("string"==typeof r.content)e=a(r.content);else if(r.content instanceof Array){let t=[];for(const e in r.content)t.push(a(r.content[e]));e=t.join("、")}break;case"og:description":n=a(r.content);break;case"og:image":i=r.content;break;default:r.content=a(r.content),o.push(r)}}return{title:this.head.title,meta:[{name:"description",content:t},{name:"keywords",content:e},{property:"og:title",content:this.head.title},{property:"og:description",content:n},{property:"og:image",content:i},...o]}}renderToString(){const t=this.getHead();let e=`<title>${t.title}</title>`;for(const n in t.meta){const a=t.meta[n];let i="",o="";if(a.name?(i="name",o=a.name):a.property&&(i="property",o=a.property),!i)return;e+=`<meta ${i}="${o}" content="${a.content}">`}const n=new Date;return e+=`<meta name="updatetime" content="${n.getFullYear().toString()+"-"+(n.getMonth()+1).toString()+"-"+n.getDate().toString()+" "+n.getHours().toString()+":"+n.getMinutes().toString()+":"+n.getSeconds().toString()}">`,e}}const o={data:()=>({_cpMetaHeadFn:e.getCurrentInstance()?.type.head}),created(){this._cpMetaHeadFn&&this.$cpMeta.setHead(this._cpMetaHeadFn())},activated(){this._cpMetaHeadFn&&this.$cpMeta.setHead(this._cpMetaHeadFn())},mounted(){this._cpMetaHeadFn&&e.watchEffect((()=>{this.$cpMeta.setHead(this._cpMetaHeadFn())}))}};t.CpMetas=i,t.cpMeta=o,t.default=function(t,e=!1,n=!1){return{install(a,s){const r=new i(t,e,n);s.mixin&&a.mixin(o),a.provide("cpMeta",r),a.config.globalProperties.$cpMeta=r}}},t.usMeta=function(t){const n=e.inject("cpMeta");n&&n.setHead(t)},Object.defineProperty(t,"__esModule",{value:!0})}));