我正在创建一个chrome扩展程序-它会自动多次填充某个Web表单。 Background.js将消息发送到content.js,效果很好。提交表单后,网站弹出一条消息,例如“表单已提交。参考号为...”,我想捕获所有这些消息。如何实现呢?
background.js
window.popups = []
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type === "myType") {
chrome.tabs.create({
url: "https://www.example.com/form"
}, function(tab) {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === tab.id && changeInfo.status == 'complete') {
chrome.tabs.onUpdated.removeListener(listener)
chrome.tabs.sendMessage(tabId, {"msg":"hello"}, function(res){})
}
})
})
}else if(request.type === "popup"){
const msg = request.msg
window.popups.push(msg)
}
}
我想在提交每个表单后调用此getPopup函数。
function getPopup(){
chrome.tabs.query({currentWindow: true, active: true},
function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {'type':'submitted'}, readPopupArr)
}
)
}
content.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.something){
}else if(request.type == "submitted"){
let msg = $(".someclass").text()
msg = msg.replace("OK", "");
chrome.runtime.sendMessage({
type:"popup", msg:msg
})
}
})