Javascript 内容脚本看到错误的文档元素
我目前正在开发一个Chrome扩展,它试图从当前选项卡中显示的页面中获取突出显示的文本 它适用于一些简单的页面,例如Wikipedia,但不适用于一些复杂的页面,尤其是像这样的主媒体页面 当我调试下面的内容脚本时,似乎文档或窗口元素与我的Chrome选项卡中显示的页面不对应 这是我的manifest.json:Javascript 内容脚本看到错误的文档元素,javascript,dom,google-chrome-extension,Javascript,Dom,Google Chrome Extension,我目前正在开发一个Chrome扩展,它试图从当前选项卡中显示的页面中获取突出显示的文本 它适用于一些简单的页面,例如Wikipedia,但不适用于一些复杂的页面,尤其是像这样的主媒体页面 当我调试下面的内容脚本时,似乎文档或窗口元素与我的Chrome选项卡中显示的页面不对应 这是我的manifest.json: { "name": "Capitalize!t2", "version": "3.1.2", "manifest_version": 2, "minimum_chrome
{
"name": "Capitalize!t2",
"version": "3.1.2",
"manifest_version": 2,
"minimum_chrome_version": "29",
"browser_action": {
"default_icon": "icon-small.png",
"default_popup": "popup.html"
},
"content_scripts": [ {
"js": [ "selectionjfm.js","jquery-1.11.1.min.js","jquery-ui.min.js" ],
"css":["jquery-ui.css"],
"matches": [ "http://*/*", "https://*/*"],
"all_frames":true
}],
"icons" : { "16": "icon-small.png",
"48": "icon.png",
"128": "icon-small.png" },
"key": "MXXXXXX",
"permissions": [
"https://secure.flickr.com/","identity", "https://accounts.google.com/*", "https://www.googleapis.com/*","https://spreadsheets.google.com/*","tabs","storage","<all_urls>"
],
"oauth2": {
"client_id": "XXXXXX",
"scopes": ["https://www.googleapis.com/auth/plus.login","https://spreadsheets.google.com/feeds"]
}
}
这是我的内容脚本(selectionjfm.cs),有点乱,我很抱歉
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
//console.log("grrroooooooove"+request.method);
var toto="";
if (request.method == "getSelection"){
var frame= document.getElementsByTagName('iframe');
if(frame!=null && frame.length>0){
for(var i=0;i<frame.length;i++){
win= frame[i].contentWindow;
idoc=win.document;
if(idoc.getSelection){
toto+=idoc.getSelection().toString();
}
}
//else console.log("no selection");
}
//console.log('totototo:'+window.getSelection().toString());
toto+=window.getSelection().toString();
toto+=document.getSelection().toString();
var html = document.getElementsByTagName('html')[0].innerHTML;
sendResponse({data: toto });
}
else{
//console.log('nullll');
sendResponse({}); // snub them.
}
});
我可以在文档中看到好的URL。referer
我想知道是否有人能解释这种重定向,以及是否有办法避免它…我不认为你被重定向了。您发布的URI来自《纽约时报》文章中嵌套的广告。您可能会发现
chrome.windows.WINDOW\u ID\u CURRENT
指的是该广告的框架,而不是父窗口
考虑在
chrome.extension.onMessage
回调函数的第一行使用调试器
语句(或),并逐步检查代码以查看出了什么问题。您正在匹配文档中的每个URL和明确地每个帧
在您打开的文档中,有一个带有该URL的广告框
当您使用chrome.tabs.sendMessage播放消息时,无法告诉API它应该转到哪个帧:该选项卡中的所有帧都会收到消息,无论它们是否正在侦听
但是,由于Chrome中消息传递的性质,只有第一次sendResponse()
调用才能成功。基本上,你有一个比赛条件
见结尾处的注释:
注意:如果多个页面正在侦听onMessage
事件,则只有第一个调用特定事件的sendResponse()
才能成功发送响应。对该事件的所有其他响应将被忽略
最简单的解决方案是从清单中删除“所有帧”:true
。如果您确实需要它,您可以(例如,使用内容脚本本身中的window.parent==window
)并基于此决定使用sendResponse
进行应答。多亏了Xan,我找到了这样一个解决方案:
在我的程序中,我知道要从中获取选择的页面/框架的URL是什么
因此,我要做的是从popup.js发送消息,同时发送我想要捕获的页面/框架的URI
function extractselection(id,**urllink**)
{
chrome.tabs.query({active:true, windowId: chrome.windows.WINDOW_ID_CURRENT},
function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "getSelection",**urllink:urllink**},
function(response){
var text = document.getElementById(id);
if(response!=null){
text.innerHTML = response.data;
}
});
});
}
在我的内容脚本中,我检查帧URL,如果它与我要查找的匹配,我将发送响应,否则不会
内容脚本:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
//console.log("grrroooooooove"+request.method);
var toto="";
if (request.method == "getSelection" && document.URL==request.urllink){
var frame= document.getElementsByTagName('iframe');
if(frame!=null && frame.length>0){
for(var i=0;i<frame.length;i++){
win= frame[i].contentWindow;
idoc=win.document;
if(idoc.getSelection){
toto+=idoc.getSelection().toString();
}
}
//else console.log("no selection");
}
//console.log('totototo:'+window.getSelection().toString());
toto+=window.getSelection().toString();
toto+=document.getSelection().toString();
var html = document.getElementsByTagName('html')[0].innerHTML;
sendResponse({data: toto });
}
// else{
//console.log('nullll');
// sendResponse({}); // snub them.
//}
});
chrome.extension.onMessage.addListener(函数(请求、发送方、发送响应){
//log(“grrrooove”+请求.method);
var toto=“”;
if(request.method==“getSelection”&&document.URL==request.urlink){
var frame=document.getElementsByTagName('iframe');
if(frame!=null&&frame.length>0){
对于(var i=0;iP.S.:currentWindow:true
比windowId:chrome.windows.WINDOW\u ID\u CURRENT
更具可读性谢谢Xan!太棒了!是的,我将在最后的代码中更改windowId;)如果你不介意的话,我已经澄清了你的问题。没问题,这是我的第一个问题,所以,我在格式上有点纠结……我也不能投你的票(需要15分)
function extractselection(id,**urllink**)
{
chrome.tabs.query({active:true, windowId: chrome.windows.WINDOW_ID_CURRENT},
function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "getSelection",**urllink:urllink**},
function(response){
var text = document.getElementById(id);
if(response!=null){
text.innerHTML = response.data;
}
});
});
}
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
//console.log("grrroooooooove"+request.method);
var toto="";
if (request.method == "getSelection" && document.URL==request.urllink){
var frame= document.getElementsByTagName('iframe');
if(frame!=null && frame.length>0){
for(var i=0;i<frame.length;i++){
win= frame[i].contentWindow;
idoc=win.document;
if(idoc.getSelection){
toto+=idoc.getSelection().toString();
}
}
//else console.log("no selection");
}
//console.log('totototo:'+window.getSelection().toString());
toto+=window.getSelection().toString();
toto+=document.getSelection().toString();
var html = document.getElementsByTagName('html')[0].innerHTML;
sendResponse({data: toto });
}
// else{
//console.log('nullll');
// sendResponse({}); // snub them.
//}
});