弹出窗口关闭时继续处理-Chrome扩展程序 [英] Continue process when popup closes - chrome extension
问题描述
我创建了一个Chrome扩展程序,该扩展程序搜索给定的单词,然后重复搜索,关闭先前找到的单词(打开弹出窗口).它工作正常,但现在我想更进一步,使其在弹出窗口关闭时继续搜索,因此我将代码传输到后台JS文件中,但是在弹出窗口弹出后它将不再循环或继续关闭.
I have created a Chrome extension that searches for a given word, and then repeats the search, dismissing words previously found (with the popup open). It works well but now I want to take it one step further and make it so it continues to search when the popup is closed, so I am transferring the code to a background JS file but it will no longer loop or continue after the popup is closed.
app.html
...<script src="jquery.js"></script>
<script src="jquery.highlight-3.js"></script>
<script src="app.js"></script>
</head>
<body>
<p id="title">Search Keyword/Terms</p>
<div id="fields">
<input type="text" id="t1" placeholder="e.g. help, buy.." />
<button class="fa fa-play" id="search_btn" ></button>
<button class="fa fa-stop" id ="clear_btn" ></button>
</div>
</body>
</html>
app.js
function search(that) {
var inputText = new String (t1.value);
var bkg = chrome.extension.getBackgroundPage();
chrome.tabs.executeScript(null,
{code:"$(document.body).highlight('"+inputText+"','"+ bkg.random_color()+"')"});
var searchButton = document.getElementById('search_btn');
searchButton.style.visibility='hidden';
var clearButton = document.getElementById('clear_btn');
clearButton.style.visibility='visible';
bkg.search_repeat(inputText);
}
document.addEventListener('DOMContentLoaded', function () {
var bkg = chrome.extension.getBackgroundPage();
var searchButton = document.getElementById('search_btn');
searchButton.addEventListener('click', search);
var clearButton = document.getElementById('clear_btn');
clearButton.addEventListener('click', hl_clear);
clearButton.style.visibility='hidden';
var searchQuery = document.getElementById('t1');
searchQuery.addEventListener('keypress', handle_keypress);
var delimField = document.getElementById('delim');
delimField.addEventListener('keypress', handle_keypress);
});
background.js
function search(that) {
var inputText = new String (t1.value);
chrome.tabs.executeScript(null,
{code:"$(document.body).highlight('"+inputText+"','"+ random_color()+"')"});
var searchButton = document.getElementById('search_btn');
searchButton.style.visibility='hidden';
var clearButton = document.getElementById('clear_btn');
clearButton.style.visibility='visible';
setInterval(function(){
async_search(inputText);
}, 2000);
}
function search_repeat(keyword) {
setInterval(function(){
reSearch(inputText);
}, 2000);
}
function reSearch(keyword) {
chrome.tabs.executeScript(null,
{code:"$(document.body).highlight('"+keyword+"','"+random_color()+"')"});
}
推荐答案
使用getBackgroundPage
时,您会调用背景页面DOM并操纵JS,但它会在前景弹出窗口中运行.您要向后台页面添加消息侦听器,以在后台页面DOM中调用该进程.
onmessage 和
When you use getBackgroundPage
you call the background pages DOM and manipulate the JS but it runs in the foreground popup. You want to add a message listener to the background page that calls the process in the background pages DOM.
onmessage and sendMessage
这将满足您的需求.
编辑
前景:
function search(that) {
var inputText = new String (t1.value);
var bkg = chrome.extension.getBackgroundPage();
chrome.tabs.executeScript(null,
{code:"$(document.body).highlight('"+inputText+"','"+ bkg.random_color()+"')"});
var searchButton = document.getElementById('search_btn');
searchButton.style.visibility='hidden';
var clearButton = document.getElementById('clear_btn');
clearButton.style.visibility='visible';
chrome.runtime.sendMessage({
search_repeat: inputText //Send method search_repeat with param inputText
});
}
和后台JS:
chrome.runtime.onMessage.addListener(function(keyword) { //Start the message listener and set an action. Add 'if's here if you want more than 1.
setInterval(function(){
reSearch(keyword.search_repeat);
}, 2000);
});
这篇关于弹出窗口关闭时继续处理-Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!