我正在使用chrome扩展程序并尝试从后台脚本向内容脚本发送消息。但我不知道为什么来自后台的消息没有传递给内容脚本。
首先,我检查后台脚本中的cookie,如果cookie可用,那么它会传递消息
PowerShell
如果没有可用的cookie,则它会通过{type:"allow_access"}
。此对象已传递给内容脚本,{type:"not_logged_in"}
决定传递给内容脚本的内容。之后,在第一种情况下调用了一个函数,在另一种情况下只调用了switch(sent.type)
方法。但不幸的是,我的代码可能存在一些问题,即阻止该邮件传递给内容脚本。
注意 - 最初的alert()
只是向后台脚本发送消息以打开标签。并且是一个单独的消息。
文档脚本
chrome.runtime.sendMessage({type:"open_login"});
后台脚本
var bact_btn = document.getElementById("openLogin");
bact_btn.addEventListener("click",function(){
chrome.runtime.sendMessage({type:"open_login"});
});
chrome.runtime.onMessage.addListener(function(sent,sender,res){
console.log("Document Script is called !");
switch(sent.type){
case "allow_access":{
alert("You are logged in !")
populate();
break;
}
case "not_logged_in":{
alert("You are not logged in to your account to use the extension ! Please login.");
break;
}
}
});
function populate(){
alert("Populate has been called ! ");
var fbEl = document.getElementsByClassName("_42nr");
var fbPost = document.getElementsByClassName("_5jmm _5pat _3lb4 s_1qgkos469b");
var fbPostUrl = document.getElementsByClassName("_5pb8 _8o _8s lfloat _ohe");
var h = '<a href="" ><div style="float:left;font-weight:bold;cursor:pointer;height:14px;color:#4b4f56;" class="myprivateLikeButton"><img src="http://bitbaysolutions.com/Icons/icon_like.png" style="width:17px;height:17px;margin-right:7px;float:left;margin-top:-4px;">Private Like</div></a>';
var currCont = 0;
window.onload = function(){
currCont = fbPost.length;
console.log(currCont);
}
for (var i = 0; i < fbEl.length ; i++) {
if((fbEl[i].children.length<=3) && !(fbEl[i].children[0].innerText.toLowerCase().trim()=="private like")){
fbEl[i].insertAdjacentHTML('afterbegin',h);
}
}
window.onscroll = function(){
if(fbPost.length>currCont){
for (var i = 0; i < fbEl.length ; i++) {
if((fbEl[i].children.length<=3) && !(fbEl[i].children[0].innerText.toLowerCase().trim()=="private like")){
fbEl[i].insertAdjacentHTML('afterbegin',h);
}
}
}
}
document.addEventListener("click",function(e){
e.preventDefault();
var ev = e.target;
if(ev.innerText=="Private Like"){
var liked = "http://bitbaysolutions.com/Icons/icon_liked.png";
ev.firstChild.setAttribute("src",liked) ;
ev.style.color="#e20505";
getIndex(fbEl,ev.parentElement.parentElement);
};
});
function getIndex(el,pr){
//Find the index value of the fb Element clicked
var index=-1;
for(var i=0;i<el.length;i++){
if(el[i]===pr){
index=i;
break;
}
}
getUrl(index);
}
function getUrl(ind){
var postInd = -1;
for(var i=0;i<fbPost.length;i++){
if(i==ind){
postInd=i;
var url = fbPost[postInd].getElementsByClassName("_5pb8 _8o _8s lfloat _ohe")[0].getAttribute("href");
chrome.runtime.sendMessage({type:"postUrl",mess:url});
}
}
}
}
清单
$(window).on('load', function(){
if (document.cookie.indexOf("_session") >= 0) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {type:"allow_access"}, function(response) {
});
});
} else{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {type:"not_logged_in"}, function(response) {
});
});
}
});
chrome.tabs.create({url:"myFrame.html"});
chrome.runtime.onMessage.addListener(function(sent,sender,res){
switch(sent.type){
case "postUrl":{
checkUrl(sent.mess);
break;
}
case "open_login":{
chrome.tabs.create({url:"myFrame.html"});
break
}
}
});
function checkUrl(url){
$.ajax({
method:"GET",
url:"http://www.bitbaysolutions.com/connections.php?fbPostUrl=true",
data:{
url:url
},
success:function(r){
alert(r);
}
});
}