新人报到
html吧
全部回复
仅看楼主
level 2
hunte_ter 楼主
Sub InjectJS_Safely() Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.Navigate "https://httpbin.org/html" ' ← 先用这个公网测试页! ' 等待加载完成 Do While ie.Busy Or ie.ReadyState <> 4 DoEvents Loop Application.Wait Now + TimeValue("00:00:02") Dim doc As Object: Set doc = ie.Document Dim script As Object Set script = doc.createElement("script") ' 要执行的 JS 代码 script.Text = " (function() { // 方法1:弹窗(用户可见) alert('✅ JS 已执行!'); // 方法2:写入标题(便于观察) document.title = '【VBA注入】ok了 - ' + new Date().toLocaleTimeString(); // 方法3:输出到控制台(需F12已打开) if (console) console.log('%c✔ ok了', 'color:green; font-weight:bold; font-size:16px;'); // 方法4:在页面上加提示文字 var notice = document.createElement('div'); notice.innerHTML = '<strong style=""background:yellow; padding:10px; position:fixed; top:10px; right:10px; z-index:9999;"">📌 我在这里!</strong>'; document.body.appendChild(notice); })(); " ' 插入到页面头部 On Error Resume Next doc.getElementsByTagName("head")(0).appendChild script If Err.Number <> 0 Then MsgBox "❌ 注入失败:" & Err.Description, vbCritical Else MsgBox "✅ JS 已成功注入,请查看页面是否有变化!", vbInformation End If Set script = Nothing Set doc = Nothing Set ie = NothingEnd Sub
2025年12月07日 13点12分 1
level 2
hunte_ter 楼主
Sub InjectJS_Safely()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "https://httpbin.org/html" ' ← 先用这个公网测试页!
' 等待加载完成
Do While ie.Busy Or ie.ReadyState <> 4
DoEvents
Loop
Application.Wait Now + TimeValue("00:00:02")
Dim doc As Object: Set doc = ie.Document
Dim script As Object
Set script = doc.createElement("script")
' 要执行的 JS 代码
script.Text = "
(function() {
// 方法1:弹窗(用户可见)
alert('✅ JS 已执行!');
// 方法2:写入标题(便于观察)
document.title = '【VBA注入】ok了 - ' + new Date().toLocaleTimeString();
// 方法3:输出到控制台(需F12已打开)
if (console) console.log('%c✔ ok了', 'color:green; font-weight:bold; font-size:16px;');
// 方法4:在页面上加提示文字
var notice = document.createElement('div');
notice.innerHTML = '<strong style=""background:yellow; padding:10px; position:fixed; top:10px; right:10px; z-index:9999;"">📌 我在这里!</strong>';
document.body.appendChild(notice);
})();
"
' 插入到页面头部
On Error Resume Next
doc.getElementsByTagName("head")(0).appendChild script
If Err.Number <> 0 Then
MsgBox "❌ 注入失败:" & Err.Description, vbCritical
Else
MsgBox "✅ JS 已成功注入,请查看页面是否有变化!", vbInformation
End If
Set script = Nothing
Set doc = Nothing
Set ie = Nothing
End Sub
2025年12月07日 13点12分 2
1