What I want to do:
I am trying to build a chrome-extension. The extension will-
text.html.Note that text.html is a fixed page, all text will be accumulated on text.html.
My Attempt:
sendMessage, something like :function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function doc_keyUp(e) {
// this would test for whichever key is 40 (down arrow) and the ctrl key at the same time
if (e.key === 'a') {
tx_s=getSelectionText();
//console.log(tx_s);
chrome.runtime.sendMessage({greeting:tx_s},function(response) {console.log(response.farewell);});
}// if (e.key === 'q')
} // doc_keyUp(e)
// register the handler
document.addEventListener('keyup', doc_keyUp, false);
it receives text by the below code:
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:request.greeting});
console.log(request.greeting);
//window.greeting[0] = request.greeting;
})
chrome.action.onClicked.addListener(function (tab) {
chrome.tabs.create({url: 'popup.html'})
})
window.bears={}; (does not allow window option).How can I transfer the text from various page to a accumlate and view on a html? Thanks.