代码拉取完成,页面将自动刷新
#!/usr/bin/python
import os
import codecs
import string
import re
# <!-- Global site tag (gtag.js) - Google Analytics -->
# <script async src="https://www.googletagmanager.com/gtag/js?id=UA-120820034-1"></script>
# <script>
# window.dataLayer = window.dataLayer || [];
# function gtag() { dataLayer.push(arguments); }
# gtag('js', new Date());
# gtag('config', 'UA-120820034-1'); // kobake 所有の Google Analytics tracking code
# </script>
# <script type="text/javascript" src="/hoge/huga/hoge.js"></script>
def replace_js_file(path, out_path, js_file):
data = '''<!-- Start Google Analytics -->
<script async type="text/javascript" src="{filename}"></script>
<!-- End Google Analytics -->
'''.format(filename=js_file)
with codecs.open(out_path, 'w', 'utf-8') as f_out:
startTag = False
with codecs.open(path, 'r', 'utf-8') as f_in:
for line in f_in:
if re.search('Google Analytics', line) and re.search('<!--', line):
startTag = True
elif startTag and re.search('</script>', line):
startTag = False
f_out.write(data)
elif not startTag:
f_out.write(line)
js_file = os.path.abspath('googleanalytics.js')
scriptdir = os.path.dirname(os.path.realpath(__file__))
for dirpath, dirnames, filenames in os.walk(scriptdir):
for file in filenames:
if file.endswith('.html'):
js_relative_path = os.path.relpath(js_file, dirpath)
js_relative_path = js_relative_path.replace('\\', '/')
path = os.path.join(dirpath, file)
out_path = path + ".tmp"
replace_js_file(path, out_path, js_relative_path)
print (js_relative_path, path)
os.remove(path)
os.rename(out_path, path)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。