Chrome插件:微信公众号自动登录(chrome.extension)
Posted 何博
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Chrome插件:微信公众号自动登录(chrome.extension)相关的知识,希望对你有一定的参考价值。
manifest.json:
{ "manifest_version": 2, "name": "WX.AutoLogin", "version": "1.0.0", "description": "微信公众号自动登录系统", "icons": { "16": "img/icon.png", "48": "img/icon.png", "128": "img/icon.png" }, "browser_action": { "default_icon": "img/icon.png", "default_title": "微信公众号自动登录系统", "default_popup": "popup.html" }, "permissions": [ "cookies", "storage", "tabs", "*://mp.weixin.qq.com/*" ], "background": { "scripts": ["js/background.js"] }, "content_scripts": [{ "matches": ["*://mp.weixin.qq.com/*"], // 多个JS按顺序注入 "js": ["js/jquery-1.8.3.js", "js/content-script.js"], // 代码注入的时间,可选值: "document_start", "document_end", or "document_idle",最后一个表示页面空闲时,默认document_idle "run_at": "document_start" }] }
popup.js:
function msg(msg) { layer.msg(msg, {offset: 0, shift:6}); } $(document).ready(function(){ chrome.storage.local.get(null, function (items) { console.log(‘items:‘, items); for (var key in items) { if (key == ‘__names‘) { continue; } $(‘#accountBtnArea‘).append(‘ <div class="button-row" data-user="‘+items[key].user+‘"> <button class="account-btn"> ‘+items[key].name+‘</button> <div class="icon-container delete-acct"><i class="fa fa-trash"></i></div> </div>‘); } }); $(‘#showAddBtn‘).click(function(event) { $(‘.button-area‘).hide(); $(‘.add-area‘).show(); $(‘.acct-input‘).val(‘‘); }); $(‘#cancelBtn‘).click(function(event) { $(‘.button-area‘).show(); $(‘.add-area‘).hide(); }); $(‘#confirmBtn‘).click(function(event) { var name = $(‘#accountName‘).val(); var user = $(‘#account‘).val(); var pwd = $(‘#password‘).val(); if (!name || !user || !pwd) { msg(‘请填入信息!‘); return; } if (name == ‘__names‘) { msg(‘该名字不允许使用!‘); return; } chrome.storage.local.get([user, ‘__names‘], function(result) { if (result[user]) { msg(‘该用户已存在,请删除后再添加‘); return; } if (result.__names && result.__names[name]) { msg(‘您已经有该名字了,请换个名字吧!‘); return; } if (!result.__names) { result.__names = {}; } $(‘#accountBtnArea‘).append(‘ <div class="button-row" data-user="‘+user+‘"> <button class="account-btn">‘+name+‘</button> <div class="icon-container delete-acct"><i class="fa fa-trash"></i></div> </div>‘); $(‘.button-area‘).show(); $(‘.add-area‘).hide(); var saveObj = {}; saveObj[user] = { user: user, pwd: md5(pwd), name: name } result.__names[name] = ‘‘; saveObj[‘__names‘] = result.__names; chrome.storage.local.set(saveObj); }); }); $(‘body‘).on(‘click‘, ‘.delete-acct‘, function(event) { $(this).closest(‘.button-row‘).remove(); var user = $(this).closest(‘.button-row‘).attr(‘data-user‘); chrome.storage.local.remove(user); chrome.storage.local.get([user, ‘__names‘], function(result) { delete result.__names[user]; chrome.storage.local.set({__names: result.__names}); }); }); $(‘body‘).on(‘click‘, ‘.button-row‘, function(event) { var bg = chrome.extension.getBackgroundPage(); bg.start($(this).data(‘user‘)); }); });
background.js:
var userStor; function removeAllCookies(cookies) { cookies.forEach(function(cookie, index) { chrome.cookies.remove({ ‘url‘:‘https://mp.weixin.qq.com‘, ‘name‘: cookie.name }); }) } function start(user) { chrome.storage.local.get([user], function(result) { if (result && result[user] && result[user].user && result[user].pwd) { userStor = result[user]; var time = Date.now()/1000|0; if (!userStor.cookies || time >= userStor.expire_time) { // 超时,需重新登陆 chrome.cookies.getAll({url: ‘https://mp.weixin.qq.com/‘}, function (cookies) { removeAllCookies(cookies); chrome.tabs.create({‘url‘: ‘https://mp.weixin.qq.com/#login‘}); }); return; } // 没有超时,直接设置cookie登陆 chrome.cookies.getAll({url: ‘https://mp.weixin.qq.com/‘}, function (cookies) { removeAllCookies(cookies); userStor.cookies.forEach(function(cookie, index) { delete cookie.hostOnly; delete cookie.session; cookie.url = ‘https://mp.weixin.qq.com/‘; chrome.cookies.set(cookie); }); // console.log(‘成功登陆公众号,准备跳转...‘) // chrome.tabs.create({‘url‘: ‘https://mp.weixin.qq.com/‘}); chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { if (tabs[0].url.startsWith(‘https://mp.weixin.qq.com‘) && tabs[0].id) { // 该页面直接跳转 chrome.tabs.sendMessage(tabs[0].id, {cmd:‘jump_url‘, url:‘https://mp.weixin.qq.com/‘}); } else { chrome.tabs.create({‘url‘: ‘https://mp.weixin.qq.com/‘}); } }); }); } else { console.log(‘请先添加账号!‘); } }); } // 监听来自content-script的消息 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.type == ‘login_success‘) { chrome.cookies.getAll({url: ‘https://mp.weixin.qq.com/‘}, function (cookies) { userStor.token = request.token; userStor.cookies = cookies; var time = Date.now()/1000|0; userStor.expire_time = time + 12*3600 - 60; var saveObj = {}; saveObj[request.user] = userStor; chrome.storage.local.set(saveObj); chrome.tabs.sendMessage(request.tabId, {cmd:‘jump_url‘, url:‘https://mp.weixin.qq.com/‘}); }); } else if (request.type == ‘content_js_start‘) { if (‘https://mp.weixin.qq.com/#login‘ == request.url) { // 创建重新登录页面返回的消息 chrome.tabs.sendMessage(sender.tab[‘id‘], {cmd:‘show_qrcode‘, user:userStor.user, pwd:userStor.pwd, tabId: sender.tab[‘id‘]}); } } });
content-script.js:
function loginWx(user, tabId) { var url = ‘https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1‘; (function wxAsk() { $.get(url, function(data, status, xhr) { if (data.ret) { console.log(data) return; } switch (data.status) { case 1: if (1 == data.user_category) { url = "https://mp.weixin.qq.com/cgi-bin/loginauth?action=ask&token=&lang=zh_CN&f=json&ajax=1"; } else { $.post(‘https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login&lang=zh_CN‘, { userlang: ‘zh_CN‘, token: ‘‘, lang: ‘zh_CN‘, f: ‘json‘, ajax: ‘1‘, }, function(data, textStatus, xhr) { if (data.base_resp && !data.base_resp.ret) { $(‘#__gzh-saomiao-status‘).text(‘登录成功!‘); var token = data.redirect_url.replace(/.*token=/g, ‘‘); chrome.runtime.sendMessage({type: ‘login_success‘, token: token, user: user, tabId: tabId}); } }); return; } break; case 2: $(‘#__gzh-saomiao-status‘).text(‘管理员拒绝!‘); return; case 3: $(‘#__gzh-saomiao-status‘).text(‘登录超时!‘); return; case 4: $(‘#__gzh-saomiao-status‘).text(‘扫描成功,等待确认...‘); break; default: $(‘#__gzh-saomiao-status‘).text(‘等待扫描...‘); break; } setTimeout(function() { wxAsk(); }, 1000); }); })(); } function start(user, pwd, tabId) { $.post(‘https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin‘, { username: user, pwd: pwd, imgcode: ‘‘, f: ‘json‘, userlang: ‘zh_CN‘, token: ‘‘, lang: ‘zh_CN‘, ajax: ‘1‘ }, function(data, status, xhr) { if (!data || !data.base_resp || data.base_resp.ret) { alert(‘用户名或密码错误!‘); console.log(data); return; } $(‘body‘).append(‘ <script>loginWx("‘+user+‘", ‘+tabId+‘);</script> <div style="width: 100%; text-align: center; position: absolute; top: 20%; background: none; z-index: 999; "> <div style="width: 20%; text-align: center; background: #389a20a3; padding: 31px; margin: 0 auto;"> <h1 style="color: #e4f31f;">请扫描二维码</h1><br> <img src="https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=getqrcode¶m=4300&rd=‘+Math.floor(1e3 * Math.random())+‘" style="width:100%;"> <br><br><h1 id="__gzh-saomiao-status" style="color: #e4f31f;">等待扫描...</h1> </div> </div> <div style="position: fixed; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0,0,0,.8); "></div> ‘); }); } // 监听来自background-script的消息 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { // console.log([‘get msg from background‘, request]); if(request.cmd == ‘show_qrcode‘) { // sendResponse(‘content js has get msg‘); start(request.user, request.pwd, request.tabId); } else if (request.cmd == ‘jump_url‘) { window.location.href = request.url; } }); // 发送消息给background js chrome.runtime.sendMessage({type: ‘content_js_start‘, url: window.location.href});
以上是关于Chrome插件:微信公众号自动登录(chrome.extension)的主要内容,如果未能解决你的问题,请参考以下文章