chrome扩展开发示例之扩展页面与content_scripts建立长连接通信
2017-03-22 11:09
507 查看
扩展和web页面之间建立长连接 只要从一端建立就可以了,既可以从web端主动建立连接也可以从扩展端主动建立连接,从两端主动建立连接区别如下:
1、从web页面端主动建立连接,就是在content_scripts文件主动建立长连接,
2、从扩展端主动建立连接
3、在任意一端监听消息都是一样的
本文例子以从扩展端主动建立连接。
2、 popup.html//扩展页面
3、 popup.js//扩展页面的js文件,在popup.html中引入
4、 index.html//测试页面,这个页面充当正常的web网页,大家可以理解为用它和扩展页面popup.html通信
5、 contentScripts.js//content_scripts文件,这个文件可以控制index.html文件中的元素,index.html与popup.html的通信,其实就是contentScripts.js与popup.js的通信。
6、 jquery.js
{
"manifest_version":
2,
"name": "扩展内部通信Demo",
"version": "1.0",
"description": "扩展内部通信Demo",
"browser_action": {
"default_popup":
"popup.html"
},
"content_scripts": [
{
"matches": ["http://localhost:63342/*"],//由于index.html是在本地运行的,这里限制了url
"js": ["js/jquery.js",
"js/contentScripts.js"]
}
]
}
popup.html:
popup.js:
//这里从扩展页面主动建立长连接,也可以从content_scripts文件主动建立长连接
index.html:
//index.html是充当web页面,所以这里内容很简单,如果contentScripts.js文件收到了popup.js页面的消息,那么他将改变web页面字体颜色
contentScripts.js:
1、从web页面端主动建立连接,就是在content_scripts文件主动建立长连接,
var port = chrome.runtime.connect({name: "haa"});//通道名称
port.postMessage({jia: "aaaa"});//发送消息
port.onMessage.addListener(function(msg) {//监听消息
});
2、从扩展端主动建立连接
chrome.tabs.query(
{active: true, currentWindow: true},
function(tabs) {
var port = chrome.tabs.connect(//建立通道
tabs[0].id,
{name: "haa"}//通道名称
);
});
3、在任意一端监听消息都是一样的
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "haa");
port.onMessage.addListener(function(msg) {
});
});
本文例子以从扩展端主动建立连接。
文档目录
1、 manifest.json//扩展入口文件2、 popup.html//扩展页面
3、 popup.js//扩展页面的js文件,在popup.html中引入
4、 index.html//测试页面,这个页面充当正常的web网页,大家可以理解为用它和扩展页面popup.html通信
5、 contentScripts.js//content_scripts文件,这个文件可以控制index.html文件中的元素,index.html与popup.html的通信,其实就是contentScripts.js与popup.js的通信。
6、 jquery.js
文件内容
manifest.json:{
"manifest_version":
2,
"name": "扩展内部通信Demo",
"version": "1.0",
"description": "扩展内部通信Demo",
"browser_action": {
"default_popup":
"popup.html"
},
"content_scripts": [
{
"matches": ["http://localhost:63342/*"],//由于index.html是在本地运行的,这里限制了url
"js": ["js/jquery.js",
"js/contentScripts.js"]
}
]
}
popup.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input id="input" type="text">
<button id="popup">popup</button>
<script src="js/jquery.js"></script>
<script src="js/popup.js"></script>// chrome不允许扩展中的HTML页面内直接内嵌js脚本,所以必须要将所有的脚本都作为外部src来引入
</body>
</html>
popup.js:
//这里从扩展页面主动建立长连接,也可以从content_scripts文件主动建立长连接
chrome.tabs.query(
{active: true, currentWindow: true},
function (tabs) {
var port = chrome.tabs.connect(//建立通道
tabs[0].id,
{name: "ma"}//通道名称
);
$("#popup").click(function () {//给web页面的按钮绑定点击事件,通过点击事件来控制发送消息
port.postMessage({jia: "aaaaaaa"});//向通道中发送消息
});
port.onMessage.addListener(function (msg) {//这里同时利用通道建立监听消息,可以监听对方返回的消息
if(msg.jia== "yuuuuu"){//如果对方(popup.js)返回的消息是{jia: "yuuuuu"}则将扩展里面的input框的值设置为"yuuuuuuu"
$('#input').val("yuuuuuuu");
};
});
});
index.html:
//index.html是充当web页面,所以这里内容很简单,如果contentScripts.js文件收到了popup.js页面的消息,那么他将改变web页面字体颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 id="h1">jiaaaaaaaaaaa</h1>
</body>
</html>
contentScripts.js:
chrome.runtime.onConnect.addListener(function (port) {//建立监听
if(port.name == "ma"){//判断通道名称
port.onMessage.addListener(function (msg) {//监听消息
if(msg.jia== " aaaaaaa "){//如果扩展发送的消息为{jia: "aaaaaaa"}那么改变字体颜色
var el = document.getElementById("h1");
if (el.style.color == 'red') {
el.style.color = 'blue';
}
else {
el.style.color = 'red';
}
port.postMessage({jia: "yuuuuu"});//向扩展同返回消息,
};
});
}
});
相关文章推荐
- Chrome的Content Scripts扩展开发
- chrome扩展开发之在content_script里执行目标页面的函数。
- chrome扩展开发之在content_script里执行目标页面的函数。
- chrome扩展中,content_scripts为页面css的图片的URL
- chrome扩展程序开发之在目标页面运行自己的JS
- chrome扩展程序开发之在目标页面运行自己的JS
- chrome扩展程序开发之在目标页面运行自己的JS
- chrome content scripts 开发
- Chrome扩展开发学习笔记之带选项页面的扩展
- 页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享
- Chrome扩展及应用开发 入门笔记(七)带选项页面的扩展与本地存储
- Chrome扩展开发指南(5)——Override Pages(重置页面)
- chrome扩展程序开发之在目标页面运行自己的JS
- chrome插件开发:content、background和popup三者之间的通信
- Chrome 扩展开发教程(3)——content_scripts用法
- chrome扩展开发简单示例
- Chrome扩展开发之二——Chrome扩展中脚本的运行机制和通信方式
- 页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享
- Chrome扩展开发 内容安全策略Content Security Policy (CSP) 官方原文部分翻译.
- chrome扩展之content scripts与web pages 交互