vuepress-plugin-baidu-seo-next
Version:
Baidu SEO plugin for vuepress v2.x
46 lines (45 loc) • 1.41 kB
JavaScript
import { onMounted } from 'vue';
import { useRouter } from '@vuepress/client';
const options = __BAIDU_SEO_OPTIONS__;
const IGNORE_LOCAL = options.ignoreLocal;
const HM = options.hm;
export const useBaiduSeo = () => {
onMounted(() => {
tongjiSubmit(1000);
const router = useRouter();
router.afterEach((to, from) => {
var toPath = to.path;
var fromPath = from.path;
if (toPath != fromPath) {
tongjiSubmit(500);
}
});
});
};
function tongjiSubmit(waitMills) {
setTimeout(() => {
tongjiPush();
}, waitMills);
}
function tongjiPush() {
if (HM && typeof window !== 'undefined') {
const host = window.location.host;
if ((host.indexOf("127.0.0.1") != -1 || host.indexOf("localhost") != -1) && IGNORE_LOCAL) {
return;
}
const id = 'baidu-tongji';
var element = document.getElementById(id);
if (element) {
element.remove();
}
window._hmt = window._hmt || [];
const hm = document.createElement('script');
hm.id = id;
hm.src = 'https://hm.baidu.com/hm.js?' + HM;
const s = document.getElementsByTagName('script')[0];
if (s.parentNode) {
s.parentNode.insertBefore(hm, s);
window._hmt.push(['_trackPageview', window.location.href]);
}
}
}