WebBrowser瀏覽器跨域訪問iframe
阿新 • • 發佈:2018-12-22
1 使用幫助類訪問iframe
public class CloseJsWindow { [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, int lParam); [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)] private static extern void SetForegroundWindow(IntPtr hwnd); //private string windowTitle = "Message from webpage"; private static string WINDOW_SAFE_TITLE = "安全警報"; private static string WINDOW_SAFE_TITLE2 = "Security Alert"; private static string BTN_YES_TITLE = "是(&Y)"; private static string BTN_YES_TITLE2 = "Yes"; private static string WINDOW_CLOSE_TITLE = "Web 瀏覽器"; private static string WINDOW_CLOSE_TITLE2 = "Windows Internet Explorer"; private static string WINDOW_JSERROR_TITLE = "指令碼錯誤"; private static string WINDOW_JSERROR_TITLE2 = "Internet Explorer 指令碼錯誤"; private static string WINDOW_JSERROR_TITLE3 = "Windows Internet Explorer Script Error"; private static string BTN_NO_TITLE = "否(&N)"; private static string BTN_NO_TITLE2 = "No"; private const uint BM_CLICK = 0xF5; //滑鼠點選的訊息,對於各種訊息的數值,大家還是得去API手冊 private const uint WM_CLOSE = 0x0010; //滑鼠點選的訊息,對於各種訊息的數值,大家還是得去API手冊 static System.Threading.Timer findTimer = null; static AutoResetEvent autoEvent = null; private static string windowTitle = string.Empty; private static string btnTitle = string.Empty; /// <summary> /// 開始監視js彈出的視窗 /// </summary> public static void StartCloseWindow() { autoEvent = new AutoResetEvent(false); //初始狀態設定為非終止 TimerCallback timerDelegate = new TimerCallback(CloseWindow); findTimer = new System.Threading.Timer(timerDelegate, autoEvent, 1000, 800); //每0.8秒鐘查詢一次 } /// <summary> /// 開始監視js彈出的視窗 /// </summary> public static void StartCloseWindow(string wTitle, string bTitle) { windowTitle = wTitle; btnTitle = bTitle; autoEvent = new AutoResetEvent(false); //初始狀態設定為非終止 TimerCallback timerDelegate = new TimerCallback(CloseWindow); findTimer = new System.Threading.Timer(timerDelegate, autoEvent, 1000, 800); //每0.8秒鐘查詢一次 } /// <summary> /// 停止監視js彈出的視窗 /// </summary> public static void StopCloseWindow() { if (autoEvent != null) { autoEvent.WaitOne(10, false); //設定10毫秒,是讓程式有一個等待,如果設為0,會一直彈視窗 } if (findTimer != null) { findTimer.Dispose(); } } private static void CloseWindow(object state) { // 處理動態引數視窗部分 if (windowTitle != null && windowTitle.Length > 0 && btnTitle != null && btnTitle.Length > 0) { IntPtr hwnd = FindWindow(null, windowTitle); //查詢視窗的控制代碼 if (hwnd != IntPtr.Zero) { IntPtr hwndSure = FindWindowEx(hwnd, 0, "Button", btnTitle); //獲取確定按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } } } // 處理安全證書提示框,預設按是 IntPtr hwnd1 = FindWindow(null, WINDOW_SAFE_TITLE); //查詢視窗的控制代碼 if (hwnd1 != IntPtr.Zero) { IntPtr hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE); //獲取確定按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } else { hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE2); //獲取確定按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } } } else { hwnd1 = FindWindow(null, WINDOW_SAFE_TITLE2); //查詢視窗的控制代碼 if (hwnd1 != IntPtr.Zero) { IntPtr hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE); //獲取確定按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } else { hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE2); //獲取確定按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } } } } // 處理IE中js呼叫window.close()彈出的關閉提示框,預設按否 IntPtr hwnd2 = FindWindow(null, WINDOW_CLOSE_TITLE); //查詢視窗的控制代碼 if (hwnd2 != IntPtr.Zero) { IntPtr hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE); //獲取否按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } else { hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE2); //獲取否按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } } } else { hwnd2 = FindWindow(null, WINDOW_CLOSE_TITLE2); //查詢視窗的控制代碼 if (hwnd2 != IntPtr.Zero) { IntPtr hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE); //獲取否按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } else { hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE2); //獲取否按鈕的控制代碼 if (hwndSure != IntPtr.Zero) { SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //傳送單擊訊息 } } } } // 處理IE中js調用出錯提示框,預設按是 IntPtr hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE); //查詢視窗的控制代碼 if (hwnd3 != IntPtr.Zero) { SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //傳送單擊訊息 } else { hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE2); //查詢視窗的控制代碼 if (hwnd3 != IntPtr.Zero) { SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //傳送單擊訊息 } else { hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE3); //查詢視窗的控制代碼 if (hwnd3 != IntPtr.Zero) { SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //傳送單擊訊息 } } } } }
2 程式碼中呼叫
IHTMLDocument3 frameWin = CorssDomainHelper.GetDocumentFromWindow(doc.frames.item(3) as IHTMLWindow2); IHTMLDocument2 fdoc = frameWin.createDocumentFragment(); IHTMLWindow2 parentWindow = fdoc.parentWindow; parentWindow.execScript(@" alert('進入'); }
經測試,只能檢視,不能修改跨域iframe裡面的doc元素