最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】gitbook中调试google-adsense插件的plugin.js看如何生效

gitbook crifan 4574浏览 0评论

折腾:

【已解决】Gitbook页面底部加google adsense广告

期间,需要去调试看看gitbook的插件google-adsense是否生效,如何生效。

后来得知核心代码是

books/gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js

结果debug出错:

info: loading plugin "google-adsense"... OK 
...
ConfigurationError: Error with book's configuration: pluginsConfig.google-adsense.ads is required

改为:

  "pluginsConfig": {

    "google-adsense": {
      "ads": [
        {
            "client": "ca-pub-6626240105039250",
            "slot": "6956288491",
            "format": "auto",
            "location": ".page-inner section"
        }
      ]
    }
  }

结果:

页面没变化:

去看看代码,研究看看如何生效

document.querySelector(c.location).appendChild(createAdElement(c));

此处:

            “location”: “.page-inner section”,

.page-inner section

是找不到的

因为有多个section

加console.log后,始终无法输出

 

换console.warn,console.error,也不行

故意把代码都搞错,结果都正常编译

看来没正常引用此库

debug: copy resources from plugin /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/generated/gitbook/node_modules/gitbook-plugin-google-adsense/book 

算了,还是去正常安装完所有插件吧

然后再去试试,能否有效。

然后继续调试,搞清楚大概逻辑了:

books/gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js

会被拷贝到debug目录下:

generated/books/gitbook_demo/debug/gitbook/gitbook-plugin-google-adsense/plugin.js

然后只要故意搞错最初的gitbook_demo下面的代码,后,再去运行:

console可以看到输出错误信息了:

那就可以继续正常代码,加log看输出了

【总结】

看来还是自己晕了。

gitbook的插件,都是js,所以输出的log,应该是去浏览器的console中找,而不是gitbook的log中找。

比如

google-adsense

对应的是:

gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js

然后就可以去添加对应的console.log了。

内部执行流程:

gitbook 去build或serve时,先去copy对应的

node_modules/gitbook-plugin-google-adsense/book/plugin.js

到目标的debug目录中

generated/books/gitbook_demo/debug/gitbook/gitbook-plugin-google-adsense/plugin.js

-》所以如果加调试代码,则是去book的node_modules中的js

node_modules/gitbook-plugin-google-adsense/book/plugin.js

才可以,否则添加的debug代码会被冲掉的。

转载请注明:在路上 » 【已解决】gitbook中调试google-adsense插件的plugin.js看如何生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.163 seconds, using 22.11MB memory