折腾:
【未解决】创建GoogleAdsense自动穿插广告单元放到crifan的gitbook的books电子书模板中
后,接着就是去想办法把自动广告的ga的js代码加到所有Gitbook的HTML中了。
感觉要去找找gitbook中,如何把上面script的js代码,加到gitbook的html的中间
要现在找之前的GoogleAdsense是如何加到html中的。
目前找到:
books/xxx/book.json
1 2 3 | "ga" : { "token" : "UA-28297199-1" }, |
crifan.com gitbook google adsense
怎么感觉是:
此处公司Mac中,没有:gitbook-plugin-google-adsense
以及还需要手动改动:
gitbook/gitbook-plugin-google-adsense/plugin.js
才可以?
gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js
然后去:
【已解决】更新Gitbook插件gitbook-plugin-google-adsense给book.crifan.com网站添加Google Adsense自动广告
另外注意到:
1 2 3 | * 在要展示广告的文章中,将广告代码放在内容的 HTML 中间 * 可能需要 20-30 分钟广告才会显示在网页上。广告最初可能会以普通的展示广告形式展示,但很快原生广告就会取而代之。 查看代码实现指南,了解更多详情。 |
即使全都正常,也要等大概半小时,才能看到广告的
只需要去:
把Gitbook的html,上传到 book.crifan.com 后,按道理,即可正常显示。
最多就是多等待一段时间后,广告即可正常加载显示,才对。
那接着,先去给gitbook_template去加上google-adsense插件,再去修改

去把配置,加过来,然后重新去初始化:
1 | gitbook install |
安装很慢,即使加了代理,好像也卡死?
1 2 3 4 | ✘ crifan@licrifandeMacBook - Pro ~ / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo master ● export HTTP_PROXY = http: / / 127.0 . 0.1 : 58591 ; export HTTPS_PROXY = http: / / 127.0 . 0.1 : 58591 ; export ALL_PROXY = socks5: / / 127.0 . 0.1 : 51837 crifan@licrifandeMacBook - Pro ~ / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo master ● gitbook installinfo: installing 17 plugins using npm@ 3.9 . 2 info: info: installing plugin "theme-comscore" |
真要是不行,就手动拷贝过来拉倒
过了会,有输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ● gitbook installinfo: installing 17 plugins using npm@3.9.2 info: info: installing plugin "theme-comscore" info: install plugin "theme-comscore" (*) from NPM with version 0.0.3 info: >> plugin "theme-comscore" installed with success info: info: installing plugin "anchors" info: install plugin "anchors" (*) from NPM with version 0.7.1 info: >> plugin "anchors" installed with success info: info: installing plugin "search-plus" info: install plugin "search-plus" (*) from NPM with version 1.0.3 info: >> plugin "search-plus" installed with success info: info: installing plugin "disqus" info: install plugin "disqus" (*) from NPM with version 0.1.0 info: >> plugin "disqus" installed with success info: info: installing plugin "prism" info: install plugin "prism" (*) from NPM with version 2.4.0 info: >> plugin "prism" installed with success info: info: installing plugin "prism-themes" info: install plugin "prism-themes" (*) from NPM with version 0.0.2 info: >> plugin "prism-themes" installed with success info: info: installing plugin "github-buttons" 。。。 |
但是的确很慢很慢。
多等等吧
突然发现,白等了,因为book.json
GitbookTemplate/gitbook_template/books/gitbook_demo/book.json
没更新:

没加:
1 | "google-adsense" , |
所以需要先去:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ✘ crifan@licrifandeMacBook - Pro ~ / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo master ● make init - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Author : crifan.com Version : 20200918 Function: Auto use gitbook to generated files: website / pdf / epub / mobi; upload to remote server; commit to your github.io repository Run 'make help' to see usage - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CURRENT_DIR = / Users / crifan / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo BOOK_NAME = gitbook_demo NOT found gitbook_demo in IGNORE_FILE_CONTENT = rm - f book.json rm - f README.md Complete load json from / Users / crifan / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo / README_current.json Complete save file / Users / crifan / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / books / gitbook_demo / README.md cp README.md . / src / README.md cp / Users / crifan / dev / dev_root / gitbook / GitbookTemplate / gitbook_template / common / config / common / common_gitignore .gitignore Complete sync content gitbook install info: installing 18 plugins using npm@ 3.9 . 2 info: info: installing plugin "google-adsense" 。。。 |
可以触发
更新book.json
再重新初始化

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | gitbook install info: installing 18 plugins using npm@3.9.2 info: info: installing plugin "google-adsense" info: install plugin "google-adsense" (*) from NPM with version 0.1.0 /Users/crifan/dev/dev_root/gitbook/GitbookTemplate/gitbook_template/books/gitbook_demo └── gitbook-plugin-google-adsense@0.1.0 info: >> plugin "google-adsense" installed with success info: info: installing plugin "theme-comscore" runTopLevelLifecycles → g ▄ ╢███████████████████████████████████████████████████████████████████████████████████░░░╟ info: install plugin "theme-comscore" (*) from NPM with version 0.0.3 info: >> plugin "theme-comscore" installed with success info: 。。。 info: runTopLevelLifecycles → g ▄ ╢██████████████████████████████████████████████████████████████████████████████░░░╟ info: install plugin "toolbar-button" (*) from NPM with version 0.0.1 info: >> plugin "toolbar-button" installed with success Compelete init all things |
等待了半小时左右,终于结束了。
此处刚才在软链接的
GitbookTemplate/gitbook_template/books/gitbook_demo/node_modules
看到的,是没有:gitbook-plugin-google-adsense
然后重启打开VSCode,即可看到:
我们希望的:
GitbookTemplate/gitbook_template/books/gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js

对应着实际的文件:
/Users/crifan/dev/dev_root/gitbook/GitbookTemplate/gitbook_template/generated/gitbook/node_modules/gitbook-plugin-google-adsense/book/plugin.js
然后去修改文件为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | require([ "gitbook" ], function(gitbook) { gitbook.events.bind( "start" , function(e, pluginConfig) { console.log( "=================== google-adsense start: pluginConfig=%o" , pluginConfig); configs = pluginConfig[ 'google-adsense' ].ads; console.log( "configs=%o" , configs); firstConfig = configs[ 0 ] firstClient = firstConfig.client console.log( "firstClient=%o" , firstClient); / / init script var adScript = document.createElement( 'script' ); adScript.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' ; adScript.setAttribute( 'async' , true); adScript.setAttribute( 'data-ad-client' , firstClient); / / add for Google Adsense Auto Ads console.log( "adScript=%o" , adScript); document.body.appendChild(adScript); }); }); |
即可。
然后去gitbook_demo中调试看看

可以看到我们要的:
1 2 3 4 5 | =================== google-adsense start: pluginConfig=。。。 firstClient= "ca-pub-6626240105039250" 。。。 adScript=<script src= "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" async= "true" data-ad-client= "ca-pub-6626240105039250" data-checked- head = "true" >< /script > 。。。 |
是正常的。
然后可以发布到book.crifan.com上去看看效果

是403的错误了,是正常的。

htm中是有我们希望的:
1 | <script src= "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" async= "true" data-ad-client= "ca-pub-6626240105039250" data-checked- head = "true" >< /script > |
比原始代码,多出了个:
data-checked-head=”true”
那接下来,就是:等待(几个小时?)ga生效,出现广告了。
转载请注明:在路上 » 【已解决】给crifan的所有gitbook的网页中加上自动广告