I am currently working on a Chrome extension that tries to get the highlighted text from the page displayed in the current tab.
It works for some simple pages, like Wikipedia for instance, but does not work for some complex pages, especially main media pages like http://www.nytimes.com/.
When I debug my content script below, it seems that the document or window element is not corresponding to the page displayed in my Chrome tab.
Here is my manifest.json :
{
"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"]
}
}
Here is the intresting part of my popup.js
function extractselection(id)
{
chrome.tabs.query({active:true, windowId: chrome.windows.WINDOW_ID_CURRENT},
function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "getSelection"},
function(response){
var text = document.getElementById(id);
if(response!=null){
text.innerHTML = response.data;
}
});
});
}
Here is then my content script (selectionjfm.cs), a bit messy I am sorry.
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.
}
});
If I go to a NY Times page, run my application and look at the debug console, I can see my document element URI is in fact:
`googleads.g.doubleclick.net/pagead/ads?...`
I can see the good URL in document.referer
.
I was wondering if any one could explain that redirection and if there was a way to avoid it...