使用JavaScript實現手機QQ在線狀態代碼實現分享前
導讀:前端開發前端開發手機QQ是一款非常流行的即時通訊應用程序,在這個應用中,我們可以看到好友的在線狀態,如在線、離線、忙碌等。這個在線狀態是通過與服務器交互來實現的,因此我們可以使網站建設seo網站優化。
手機QQ是一款非常流行的即時通訊應用程序,在這個應用中,我們可以看到好友的在線狀態,如在線、離線、忙碌等。這個在線狀態是通過與服務器交互來實現的,因此我們可以使用JavaScript模擬這個過程,以獲取好友的在線狀態。
以下是一份基于JavaScript的手機QQ在線狀態查詢代碼示例:
// 配置變量 var account = "your_account"; // 填寫要查詢的好友賬號 var loginUrl = "http://ptlogin2.qq.com/login"; // QQ登錄頁面的URL var checkUrl = "http://d1.web2.qq.com/channel/poll2"; // QQ查詢好友狀態的URL var appId = "1003903"; // 應用ID號 var clientVer = "0.0.1"; // 應用版本號 var psessionid = ""; // 會話ID號 var vfwebqq = ""; // vfwebqq參數 var pollingInterval = 5000; // 檢查好友狀態的時間間隔,單位為毫秒 // 使用JSONP獲取QQ登錄頁面的COOKIE信息 function getQqLoginCookie() { var script = document.createElement('script'); script.src = loginUrl + '?appid=' + appId + '&js_type=0&login_type=3&'; script.type = 'text/javascript'; script.async = true; document.body.appendChild(script); } // 從COOKIE中提取ptwebqq和uin參數 function getCookie(key, content) { var regex = new RegExp('\\b' + key + '\\s*=([^;]*)', 'i'); var match = regex.exec(content || document.cookie); return match && match[1]; } // 登錄QQ并獲取Cookie信息 function loginQq(username, password) { var iframe = document.createElement("iframe"); iframe.src = "about:blank"; document.body.appendChild(iframe); var form = iframe.contentDocument.createElement("form"); form.method = "post"; form.action = loginUrl; var input1 = iframe.contentDocument.createElement("input"); input1.type = "hidden"; input1.name = "u"; input1.value = username; form.appendChild(input1); var input2 = iframe.contentDocument.createElement("input"); input2.type = "hidden"; input2.name = "p"; input2.value = password; form.appendChild(input2); var input3 = iframe.contentDocument.createElement("input"); input3.type = "hidden"; input3.name = "verifycode"; input3.value = ""; form.appendChild(input3); var input4 = iframe.contentDocument.createElement("input"); input4.type = "hidden"; input4.name = "webqq_type"; input4.value = "10"; form.appendChild(input4); var input5 = iframe.contentDocument.createElement("input"); input5.type = "hidden"; input5.name = "remember_uin"; input5.value = "1"; form.appendChild(input5); var input6 = iframe.contentDocument.createElement("input"); input6.type = "hidden"; input6.name = "login2qq"; input6.value = "1"; form.appendChild(input6); var input7 = iframe.contentDocument.createElement("input"); input7.type = "hidden"; input7.name = "aid"; input7.value = appId; form.appendChild(input7); var input8 = iframe.contentDocument.createElement("input"); input8.type = "hidden"; input8.name = "u1"; input8.value = "http://w.qq.com/proxy.html?login2qq=1&webqq_type=10"; form.appendChild(input8); var input9 = iframe.contentDocument.createElement("input"); input9.type = "hidden"; input9.name = "ptredirect"; input9.value = "0"; form.appendChild(input9); var input10 = iframe.contentDocument.createElement("input"); input10.type = "hidden"; input10.name = "ptlang"; input10.value = "2052"; form.appendChild(input10); var input11 = iframe.contentDocument.createElement("input"); input11.type = "hidden"; input11.name = "daid"; input11.value = "164"; form.appendChild(input11); var input12 = iframe.contentDocument.createElement("input"); input12.type = "hidden"; input12.name = "from_ui"; input12.value = "1"; form.appendChild(input12); var input13 = iframe.contentDocument.createElement("input"); input13.type = "hidden"; input13.name = "pttype"; input13.value = "1"; form.appendChild(input13); var input14 = iframe.contentDocument.createElement("input"); input14.type = "hidden"; input14.name = "dumy"; input14.value = "公司網站建設"; form.appendChild(input14); iframe.contentDocument.body.appendChild(form); form.submit(); } // 初始化會話信息 function initSessionInfo() { // 從登錄成功的COOKIE中解析出ptwebqq和uin參數 var ptwebqq = getCookie("ptwebqq"); var qqUin = getCookie("uin"); qqUin = qqUin.substring(1, qqUin.length - 1); // 請求一個隨機值獲取vfwebqq參數 var xhr = new XMLHttpRequest(); xhr.open("GET", "http://s.web2.qq.com/api/getvfwebqq?ptwebqq=" + ptwebqq + "&clientid=seo網站優化軟件" + appId + "&psessionid=&t=" + (new Date()).valueOf() + "&vfwebqq=", false); xhr.send(); // 從JSON結果中解析出vfwebqq參數 var resp = eval("(" + xhr.responseText + ")"); vfwebqq = resp["result"]["vfwebqq"]; // 請求ptwebqq和psessionid參數 xhr = new XMLHttpRequest(); xhr.open("GET", checkUrl + "?clientid=" + appId + "&psessionid=&t=" + (new Date()).valueOf() + "&vfwebqq=" + vfwebqq, false); xhr.send(); // 從JSON結果中解析出ptwebqq和psessionid參數 resp = eval("(" + xhr.responseText + ")"); psessionid = resp["result"]["psessionid"]; } // 獲取好友的JSON字符串 function getFriendJson() { var xhr = new XMLHttpRequest(); xhr.open("POST", checkUrl + "?clientid=" + appId + "&psessionid=" + psessionid + "&t=" + (new Date()).valueOf(), false); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); xhr.send("r={\"ptwebqq\":\"" + getCookie("ptwebqq") + "\",\"clientid\":\"" + appId + "\",\"psessionid\":\"" + psessionid + "\",\"key\":\"\"}"); return xhr.responseText; } // 解析好友JSON字符串,返回好友在線狀態 function getFriendStatus(friendJson, friendAccount) { var decoded = eval("(" + friendJson + ")"); var friends = decoded["result"][0]["value"]["info"]; for (var i = 0; i < friends.length; i++) { var friend = friends[i]; if (friend["uin"] == friendAccount) { return friend["stat"]["name"]; } } return ""; } // 定時查詢好友狀態 function pollFriendStatus() { var friendJson = getFriendJson(); var friendStatus = getFriendStatus(網站seo優化課程friendJson, account); console.log("Friend " + account + " is " + friendStatus); setTimeout(pollFriendStatus, pollingInterval); } // 執行查詢好友狀態的主函數 function queryFriendStatus() { getQqLoginCookie(); // 獲取QQ登錄頁面的COOKIE信息 loginQq("your_username", "your_password"); // 使用QQ賬戶登錄 initSessionInfo(); // 初始化會話信息 pollFriendStatus(); // 定時查詢好友狀態 } queryFriendStatus(); // 運行主程序聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-27 ,文章使用JavaScript實現手機QQ在線狀態代碼實現分享前主要講述在線,狀態,手機網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_11191.html