0
我在擴展構建新的,對愚蠢的問題很抱歉: 我需要捕獲文本從活動選項卡(例如,從段落標籤中的所有文本,將其發送到擴展。之後,擴展將文本發送到其他地方(這不是問題)。我使用彈出窗口僅用於開/關擴展和重新發送文本。我需要,該文本將自動發送後,用戶進入浴缸(不按到擴展圖標) 在popup.js我添加代碼:Chrome擴展:如何捕獲文本,而不使用彈出
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() - 函數到文本發送
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);
的問題,只有當我按下擴展圖標,我必須這樣做,無需按代碼工作。我明白,總有一件事容易,我錯過了,但我不知道什麼:) 可能是一個知道解決方案(希望如此)。 非常感謝!