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

【已解决】给crifan的所有gitbook的网页中加上自动广告

gitbook crifan 1081浏览 0评论
折腾:
【未解决】创建GoogleAdsense自动穿插广告单元放到crifan的gitbook的books电子书模板中
后,接着就是去想办法把自动广告的ga的js代码加到所有Gitbook的HTML中了。
感觉要去找找gitbook中,如何把上面script的js代码,加到gitbook的html的中间
要现在找之前的GoogleAdsense是如何加到html中的。
目前找到:
books/xxx/book.json
    "ga": {
      "token": "UA-28297199-1"
    },
crifan.com gitbook google adsense
【已解决】Gitbook页面底部加google adsense广告 – 在路上
怎么感觉是:
此处公司Mac中,没有:gitbook-plugin-google-adsense
以及还需要手动改动:
gitbook/gitbook-plugin-google-adsense/plugin.js
才可以?
【已解决】gitbook中安装Google Adsense插件:gitbook-plugin-google-adsense – 在路上
【已解决】gitbook中调试google-adsense插件的plugin.js看如何生效 – 在路上
gitbook_demo/node_modules/gitbook-plugin-google-adsense/book/plugin.js
【已解决】gitbook的Adsense插件报错:Failed to load resource the server responded with a status of 403 – 在路上
【已解决】Google Adsense中创建广告单元和部署广告单元 – 在路上
然后去:
【已解决】更新Gitbook插件gitbook-plugin-google-adsense给book.crifan.com网站添加Google Adsense自动广告
另外注意到:
概览 – Google AdSense
* 在要展示广告的文章中,将广告代码放在内容的 HTML 中间
* 可能需要 20-30 分钟广告才会显示在网页上。广告最初可能会以普通的展示广告形式展示,但很快原生广告就会取而代之。
查看代码实现指南,了解更多详情。
即使全都正常,也要等大概半小时,才能看到广告的
只需要去:
把Gitbook的html,上传到 book.crifan.com 后,按道理,即可正常显示。
最多就是多等待一段时间后,广告即可正常加载显示,才对。
那接着,先去给gitbook_template去加上google-adsense插件,再去修改
去把配置,加过来,然后重新去初始化:
gitbook install
安装很慢,即使加了代理,好像也卡死?
 ✘ 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 [email protected] 
info:  
info: installing plugin "theme-comscore" 
真要是不行,就手动拷贝过来拉倒
过了会,有输出:
●  gitbook installinfo: installing 17 plugins using [email protected] 
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
没更新:
没加:
"google-adsense",
所以需要先去:
 ✘ 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 [email protected] 
info:  
info: installing plugin "google-adsense" 
。。。
可以触发
更新book.json
再重新初始化
gitbook install
info: installing 18 plugins using [email protected] 
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
└── [email protected] 


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
然后去修改文件为:
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中调试看看
可以看到我们要的:
=================== 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中是有我们希望的:
<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的网页中加上自动广告

发表我的评论
取消评论

表情

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

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