chrome插件自动登录

实现功能

本文代码为开发调试demo

  • 实现功能:点击插件按钮后,自动填充账号密码并点击登录按钮。
    方式一:popup->background->content
    方式二:popup->content
    1. 不同按钮对应不同账号
    2. 点击按钮,自动填充账号密码并点击登录按钮
    3. background 和content互发消息
  • 测试用的登录页面:B站登录页面
  • 测试用的浏览器:
    1. Chrome版本 76.0.3809.132 (正式版本) (64 位)
    2. Edge 版本 92.0.902.84 (官方内部版本) (64 位)

在这里插入图片描述
在这里插入图片描述

工程结构

需要以下文件:

1. manifest.json

插件配置1

2. popup.html

不同按钮对应不同账号2

<head>
    <!--中文乱码问题-->
    <meta charset="UTF-8">
    <meta name="kyujyu" content="Hege Refsnes">
</head>
<style>
    body {
        overflow: hidden;
        margin: 0px;
        padding: 0px;
        background: #00a7de;
    }

    button:first-child {
        margin-top: 0px;
    }

    button {
        cursor: pointer;
        text-align: center;
        padding: 3px 0px 3px 0px;
        font-family: sans-serif;
        font-size: 0.8em;
        width: 100px;
        color: #C8CDE1;
        background: #00a7de;
        border-radius: 4px;
        border: none;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    button:hover {
        color: white;
        background: #0381aa;
    }
</style>
<!--id:账号 value:密码-->
<button id="user0" value="test">账号0</button>
<button id="user1" value="password">user1</button>
<button id="user2" value="pwd">user2</button>
<script src="./popup.js"></script>

3. popup.js

点击按钮,将账号密码传给background处理(方式一) 3
或者直接发消息给content(方式二)

divsList = document.getElementsByTagName("button");
for (let i =0; i< divsList.length; i++){
	const username = divsList[i].id;
	const password = divsList[i].getAttribute("value");
	divsList[i].onclick = (function (i){
		return function(){
			login(username, password);
			}
		}
	)(i);
	divsList[i].title = "账号:"+username+"密码:"+password;
}

function login(usr, pwd) {
	//方式一: 直接调background中的方法
    // var bg2 = chrome.extension.getBackgroundPage();
    // bg2.bglogin(usr,pwd);//bglogin(usr,pwd)是background中的方法

    //方式二:发消息给content  popup->content
    var msg = {'usr':usr,'pwd':pwd+"frompopup.js"};
    chrome.tabs.query({active: true}, function(tabs) {
        console.log("popup->content");
        console.log("tabs",tabs)
        console.log("tabs[0].id",tabs[0].id)
        chrome.tabs.sendMessage(tabs[0].id, msg, function(response) {
            console.log(response);
        });
    });
};

4. background.js

发送消息给content,监听content的消息4 5 6

function bglogin(usr, pwd) {
    var msg = {'usr':usr,'pwd':pwd};
    //发消息
    chrome.tabs.query({active: true}, function(tabs) {
        console.log("tabs",tabs)
        console.log("tabs[0].id",tabs[0].id)
        chrome.tabs.sendMessage(tabs[0].id, msg, function(response) {
            console.log(response);
        });
    });
}


//监听消息
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
    const res = req.info;
    console.log("received:"+res);
    sendResponse("backresponse3424324");
    return true;
})

5. content.js

发消息给background
监听消息
填充input点击登录7


window.inputValue = function(dom, st){
    var evt = new InputEvent("input", {
        inputType: "insertText",
        data:st,
        dataTransfer:null,
        isComposing:false
    });
    dom.value = st;
    dom.dispatchEvent(evt);
}


function autologin(user, pwd){
    var username = document.getElementById("login-username");
    username.value = user;
    window.inputValue(username, username.value);

    var password = document.getElementById("login-passwd");
    password.value = pwd;
    window.inputValue(password, password.value);

    var button = document.getElementsByClassName("btn btn-login")[0];
    button.click();
}

//监听消息   content登录
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
    // alert("usr:"+message.usr+"pwd:"+message.pwd);
    autologin(message.usr, message.pwd);
    sendResponse("true");
    return true;
});


//-------------------------------------------------------------------------------
// console.log("this is content.js!");
// //发消息  content->background
// chrome.runtime.sendMessage({
//     info: "content->background"
// }, res => {
//     // 答复
//     alert(res);
// });
//

6. img 放图片

调试

chrome插件开发之调试

Firefox插件

除了导入方式不同,其他都一样
Firefox插件(拓展)开发

参考资料


  1. manifest.json文件配置 ↩︎

  2. chrome扩展的browseraction:popup页面与js ↩︎

  3. js添加onclick函数 ↩︎

  4. Chrome插件中 popup,background,contantscript消息传递机制 ↩︎

  5. 谷歌浏览器扩展程序报错 The message port closed before a response was received. ↩︎

  6. chrome插件开发-消息机制中的bug与解决方案 ↩︎

  7. js模拟实现输入框input事件 ↩︎

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值