例如,我在網頁里有一個<script> alert(1) <script>。現在我想做的是,在它被加載后和執行前,立即將其改為<script> alert(2) <script>。我能成功嗎?
uj5u.com熱心網友回復:
簡短的回答是不能;只有在頁面被提供之前進行修改才行
正如你所看到的,你不能用script攔截腳本
。它將在決議時被執行
。 。window。 addEventListener("DOMContentLoaded",function() {
document.scripts[1].textContent = " ";
alert(2)
})
< script>alert(1)</script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
這里有一個黑客技術
window。 addEventListener("DOMContentLoaded",function() {
alert(3)
})
<script>//如果你能插入這個之前可以攔截警報
const saveAlert = window.alert。
window.alert = function(str) {
saveAlert(2)
window.alert = saveAlert;
};
</script>>
<script>alert(1)</script>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
證明另一個答案不起作用
。var h = document. getElementById('hello'/span>)。
h.textContent = 'alert(2)'
< script type='text/javascript' id='hello'>
alert(1)
</script>/span>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
假設你像這樣設定你的腳本標簽
< script type='text/javascript' id='hello'>
alert(1)
</script>
然后你可以這樣獲得dom元素:
var h = document.getElementById('hello') 。
然后你可以這樣修改元素:
h.innerHTML = 'alert(2)'
我還建議你使用類似JQuery onready的東西,以確保修改代碼在腳本標簽中的警報之前執行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311289.html
標籤:
上一篇:unity學習筆記(持續更新)
