修改容器源码使其可以动态操作
服务端
2021年3月31日星期三 10:37
# 注入脚本
<script type="text/javascript">
window.onmessage = function(e){
if (e.data) {
var x = document.createElement("SCRIPT");
x.setAttribute("type","text/javascript");
x.text = e.data;
document.body.appendChild(x);
}
}
</script>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
这里以
grafana
为例
# 通过替换注入
sed -i 's/<\/body>/<script type="text\/javascript">window.onmessage = function(e){ if (e.data) { var x = document.createElement("SCRIPT"); x.setAttribute("type","text\/javascript"); x.text = e.data; document.body.appendChild(x); } } <\/script><\/body>/g' /usr/share/grafana/public/views/index.html
1
# k8s 配置
可以如下:
containers:
- args:
- --config=/etc/grafana/config/custom.ini
image: grafana/grafana:7.4.0
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command:
- /bin/bash
- -c
- |
sed -i 's/<\/body>/<script type="text\/javascript">window.onmessage = function(e){ if (e.data) { var x = document.createElement("SCRIPT"); x.setAttribute("type","text\/javascript"); x.text = e.data; document.body.appendChild(x); } } <\/script><\/body>/g' /usr/share/grafana/public/views/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 调用
实际的业务代码中可以这样写
window.document.getElementsByTagName('iframe')[0].contentWindow.postMessage(`(function(){
document.querySelector('.sidemenu').style.display='none';
document.querySelector('.navbar-page-btn').style.display='none';
document.querySelector('.navbar-buttons.navbar-buttons--tv').style.display='none';
})();`, '*')
1
2
3
4
5
2
3
4
5