|
1 | 1 | /* |
2 | 2 | * @Author: Rainy |
3 | 3 | * @Date: 2019-12-01 14:25:59 |
4 | | - * @LastEditors: Rainy |
5 | | - * @LastEditTime: 2019-12-05 20:38:59 |
| 4 | + * @LastEditors : Rainy |
| 5 | + * @LastEditTime : 2020-02-07 12:53:01 |
6 | 6 | */ |
7 | 7 |
|
8 | | -function tryRun (fn, times = 3) { |
9 | | - let execCount = 1; |
10 | | - fn(next); |
11 | | - function next(delay) { |
12 | | - if (execCount >= times) { |
13 | | - return; |
14 | | - } |
15 | | - setTimeout(() => { |
16 | | - execCount += 1 |
17 | | - fn(next) |
18 | | - }, delay); |
19 | | - } |
20 | | -} |
21 | | - |
22 | | -function integrateGitalk(router) { |
23 | | - const linkGitalk = document.createElement('link'); |
24 | | - linkGitalk.href = 'https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css'; |
25 | | - linkGitalk.rel = 'stylesheet'; |
26 | | - document.body.appendChild(linkGitalk); |
27 | | - const scriptGitalk = document.createElement('script'); |
28 | | - scriptGitalk.src = 'https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js'; |
29 | | - document.body.appendChild(scriptGitalk); |
| 8 | +// import copy from './utils/copy'; |
| 9 | +import getGitalk from './utils/getGitalk'; |
30 | 10 |
|
31 | | - router.afterEach((to, from) => { |
32 | | - |
33 | | - if (scriptGitalk.onload) { |
34 | | - loadGitalk(to); |
35 | | - } else { |
36 | | - scriptGitalk.onload = () => { |
37 | | - loadGitalk(to); |
38 | | - }; |
| 11 | +export default ({ Vue, options, router, siteData }) => { |
| 12 | + setTimeout(() => { |
| 13 | + try { |
| 14 | + //对document的判断是防止编译的时候报错 |
| 15 | + document && |
| 16 | + (() => { |
| 17 | + getGitalk({ router }); |
| 18 | + // copy(); |
| 19 | + })(); |
| 20 | + } catch (e) { |
| 21 | + console.error(e.message); |
39 | 22 | } |
40 | | - }); |
41 | | - |
42 | | - function loadGitalk(to) { |
43 | | - let commentsContainer = document.getElementById('gitalk-container'); |
44 | | - if (!commentsContainer) { |
45 | | - commentsContainer = document.createElement('div'); |
46 | | - commentsContainer.id = 'gitalk-container'; |
47 | | - commentsContainer.classList.add('content'); |
48 | | - } |
49 | | - const $page = document.querySelector('.page'); |
50 | | - if ($page) { |
51 | | - $page.appendChild(commentsContainer); |
52 | | - if (typeof Gitalk !== 'undefined' && Gitalk instanceof Function) { |
53 | | - renderGitalk(to.fullPath); |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - function renderGitalk(fullPath) { |
58 | | - const gitalk = new Gitalk({ |
59 | | - clientID: '58efa883d352424befd3', |
60 | | - clientSecret: 'faf0e2d7ff9355b3692d605d698f7abcbbd43388', |
61 | | - repo: 'https://github.com/Rain120/awesome-javascript-code-implementation', |
62 | | - owner: 'rain120', |
63 | | - admin: ['rain120'], |
64 | | - id: window.location.pathname || fullPath || 'comment', |
65 | | - distractionFreeMode: false, |
66 | | - language: 'zh-CN' |
67 | | - }); |
68 | | - gitalk.render('gitalk-container'); |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -export default ({ Vue, options, router }) => { |
73 | | - try { |
74 | | - document && integrateGitalk(router); |
75 | | - } catch (e) { |
76 | | - console.error(e.message); |
77 | | - } |
| 23 | + }, 500); |
78 | 24 | }; |
0 commit comments