1 | const { resolve } = require('path')
|
2 |
|
3 |
|
4 | const Defaults = {
|
5 | tag: 'adsbygoogle',
|
6 | id: null,
|
7 | pageLevelAds: false,
|
8 | includeQuery: false,
|
9 | analyticsUacct: '',
|
10 | analyticsDomainName: '',
|
11 | overlayBottom: false,
|
12 | test: false,
|
13 | onPageLoad: false
|
14 | }
|
15 |
|
16 |
|
17 | const TestID = 'ca-google'
|
18 |
|
19 |
|
20 | const AdSenseURL = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
|
21 |
|
22 | module.exports = function nuxtAdSense (moduleOptions = {}) {
|
23 | const options = Object.assign({}, Defaults, this.options['google-adsense'] || moduleOptions)
|
24 |
|
25 |
|
26 | options.test = Boolean(options.test)
|
27 | options.pageLevelAds = Boolean(options.pageLevelAds)
|
28 | options.includeQuery = String(Boolean(options.includeQuery))
|
29 | options.analyticsUacct = options.analyticsUacct || ''
|
30 | options.analyticsDomainName = options.analyticsDomainName || ''
|
31 | options.overlayBottom = Boolean(options.overlayBottom)
|
32 | options.onPageLoad = Boolean(options.onPageLoad)
|
33 |
|
34 | if (this.options.dev && process.env.NODE_ENV !== 'production') {
|
35 |
|
36 |
|
37 | options.test = true
|
38 | }
|
39 |
|
40 | if (options.test) {
|
41 |
|
42 | options.id = TestID
|
43 | }
|
44 |
|
45 | if (!options.id || typeof options.id !== 'string') {
|
46 |
|
47 |
|
48 | return
|
49 | }
|
50 |
|
51 |
|
52 | options.tag = options.tag || Defaults.tag
|
53 |
|
54 |
|
55 | this.addPlugin({
|
56 | src: resolve(__dirname, './plugin.template.js'),
|
57 | fileName: 'adsbygoogle.js',
|
58 | options: options
|
59 | })
|
60 |
|
61 |
|
62 | this.options.head.script.push({
|
63 | async: true,
|
64 | src: AdSenseURL
|
65 | })
|
66 |
|
67 |
|
68 | this.options.head.__dangerouslyDisableSanitizersByTagID = this.options.head.__dangerouslyDisableSanitizersByTagID || {}
|
69 | this.options.head.__dangerouslyDisableSanitizersByTagID.adsbygoogle = ['innerHTML']
|
70 |
|
71 | const adsenseScript = `{
|
72 | google_ad_client: "${options.id}",
|
73 | enable_page_level_ads: ${options.pageLevelAds ? 'true' : 'false'},
|
74 | overlays: {bottom: ${options.overlayBottom}}
|
75 | }`
|
76 |
|
77 | if (!options.onPageLoad) {
|
78 | this.options.head.script.push({
|
79 | hid: 'adsbygoogle',
|
80 | innerHTML: `
|
81 | (window.adsbygoogle = window.adsbygoogle || []).push(${adsenseScript});`
|
82 | })
|
83 | } else {
|
84 | this.options.head.script.push({
|
85 | hid: 'adsbygoogle',
|
86 | innerHTML: `
|
87 | (window.adsbygoogle = window.adsbygoogle || []).onload = function () {
|
88 | [].forEach.call(document.getElementsByClassName('adsbygoogle'), function () {
|
89 | adsbygoogle.push(${adsenseScript});
|
90 | })
|
91 | }`
|
92 | })
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | if (options.test) {
|
98 | this.options.head.meta.unshift({
|
99 | name: 'robots',
|
100 | content: 'noindex,noarchive,nofollow'
|
101 | })
|
102 | }
|
103 | }
|
104 |
|
105 | module.exports.meta = require('./../package.json')
|