Reputation: 431
I'm new in extension building, so sorry for stupid question: I need capture the text from active tab (for example all text from the paragraph tags, send it to extension. After that the extension will send the text to other place( this isn't problem). I use the popup window only for on/off extension and resending text. I need that, the text will send automatically after that user enter to tub (without pressing to extension icon). In the popup.js i add the code:
popup.js
document.addEventListener('DOMContentLoaded',function() {
chrome.windows.getCurrent(function (currentWindow) {
chrome.tabs.query({active: true, windowId: currentWindow.id},
function(activeTabs) {
chrome.tabs.executeScript(
activeTabs[0].id, {file: 'tabInjection.js', allFrames: true});
});
});
});
chrome.extension.onRequest.addListener(function(jsonText) {
sendToOtherServer(jsonText);
});
sendToOtherServer()- function to text sending
tabInjection.js
var currentText = [].slice.apply(document.getElementsByTagName('p'));
console.log(currentText[0]);
var clearText = [];
for ( var index in currentText) {
console.log(currentText[index].innerHTML);
clearText.push(currentText[index].innerHTML + "<br>");
}
var jsonText = JSON.stringify(clearText);
chrome.extension.sendRequest(jsonText);
The problem, that the code work only if i press extension icon, and i need to do this without pressing. I'm understand that there is sum thing easy, that i missed, but i don't know what:) May be sum one know solution (hope so). Thank a lot!
Upvotes: 0
Views: 210
Reputation: 99
Chrome extension content scripts may be able to do what you want. Here is a link to how to use them: https://developer.chrome.com/extensions/content_scripts
Upvotes: 1