Chrome, get selected text issue
I have made an extension for chrome that relies on getting the selected text of a page.
Thanks to some of the questions on here, I have managed to do this but it seems to be spotty, only working on 'most' pages.
Any ideas why:
sendResponse({data: window.getSelection().toString()});
in my selection.js only works on some sites and does not work on, for example:
http://www.tdwg.org/ ?
Is it simply something to do with the page structure and not worth handling?
EDIT: I can definitely isolate the issue to this code as the code that calls this simple grabs the selected text and opens a new tab with this as an argument for the URL. I have tried text with special characters and it all works fine. However, this is the function to open the new tab and the listener that has been added to the extension.
function sendServiceRequest(selected) {
selected = selected.trim();
var text = selected.replace(/s+/g,"_");
chrome.tabs.create({'url': chrome.extension.getURL('reader.html?text1=' + escape(text))}, function(tab) {
// Tab opened.
});
}
// Set the background page to listen for click events on the brower_action
// button. Listener then calls updateIcon() whenever triggered.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function(response){
sendServiceRequest(response.data);
});
});
Thanks in advance!
链接地址: http://www.djcxy.com/p/88018.html下一篇: Chrome,获取选定的文本问题