插件通信 bacoground.js

 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
		console.log(sender.tab ?"from a content script:" + sender.tab.url : "from the extension");  
		if(request.greeting == "hello")
			sendResponse({farewell: "这是background.js发出的farewell "+JSON.stringify(sender.tab.id)});
	});
        //只要当前页面不关闭,那tab.id就不会变;关闭再新开,tab.id就变成新的了
	//但经过测试,刷新页面,frameId会变
	
	//打开百度翻译,JSON.stringify(sender) 返回的是下面的值
/*  {
		"id": "pknlpddjpejdkiebokcdcfdekpaeejnh",	//这是插件的id
		"url": "http://fanyi.baidu.com/translate?aldtype=16047&query=audible&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/audible", //当前 网页 或 iframe 的网址,和下面的url地址有可能不是同一个
		"tab": {
			"active": true,
			"audible": false,
			"autoDiscardable": true,
			"discarded": false,
			"favIconUrl": "http://fanyi.bdstatic.com/static/translation/img/favicon/favicon_d87cd2a.ico",
			"height": 921,
			"highlighted": true,
			"id": 94,
			"incognito": false,
			"index": 8,
			"mutedInfo": {
				"muted": false
			},
			"pinned": false,
			"selected": true,
			"status": "loading",
			"title": "百度翻译",
			"url": "http://fanyi.baidu.com/translate?aldtype=16047&query=audible&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/audible", //当前网页的网址
			"width": 1354,
			"windowId": 2
		},
		"frameId": 0
	}
*/


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的猫叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值