Javascript Chrome Extension-background.js可以访问本地html页面,当它';这不是活动选项卡吗?
当用户单击my chrome扩展名popup.html中的按钮时,它会打开form.html,其中有一个文本区域,用户可以在其中输入文本。在用户将文本输入文本区域后,我希望background.js访问输入的文本。当用户切换选项卡时,我希望background.js获取文本区域中的内容,即使form.html不是活动选项卡 我已通过单击按钮将document.getElementById(“textarea”).value作为消息传递给background.js,但我希望background.js在form.html不是活动选项卡时获得该值(并最终删除该按钮) popup.htmlJavascript Chrome Extension-background.js可以访问本地html页面,当它';这不是活动选项卡吗?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,当用户单击my chrome扩展名popup.html中的按钮时,它会打开form.html,其中有一个文本区域,用户可以在其中输入文本。在用户将文本输入文本区域后,我希望background.js访问输入的文本。当用户切换选项卡时,我希望background.js获取文本区域中的内容,即使form.html不是活动选项卡 我已通过单击按钮将document.getElementById(“textarea”).value作为消息传递给background.js,但我希望background.j
<!DOCTYPE html>
<html style=''>
<head>
<head><title>activity</title></head>
</head>
<body style="width:600px;">
<div id="myText"></div><p>
<button type="button" class="button" id="btn1">Input info</button>
<script src="popup.js"></script>
</body>
</html>
form.html
<style>
html, body {
}
#myForm {
width: 100%;
height: 100%;
margin: auto;
position: relative;
padding: 0; }
</style>
<body>
<div class="form-popup" id="myForm">
<form id="s3paths" class="form-container">
<h2>Enter info here</h2><p>
<textarea rows="5" cols="80" id="textarea" placeholder = "example of what to input">
</textarea></p>
<button type="button" class="button" id="btn2">Load</button>
<script src="form.js"></script>
</form>
</div>
</body>
background.js
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
alert(msg.paths);
});
});
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
var paths = msg.paths;
chrome.storage.sync.set({key: paths}, function() {
//alert('Value is set to ' + paths);
});
});
});
如何让background.js从用户切换到的任何选项卡访问document.getElementById(“textarea”).form.html的值?更新:我找到了解决方法。在我将文本区域中的值作为消息从form.js传递到background.js之后,我使用chrome.storage.sync来存储它,因此当我转到其他选项卡时,它仍然被存储。(我一直按着按钮) form.js
var button = document.getElementById("btn2");
button.addEventListener("click", function(){
alert('loaded!');
var port = chrome.runtime.connect({name: "paths"});
var spaths = document.getElementById("textarea").value;
port.postMessage({paths: spaths});
});
var button = document.getElementById("btn2");
button.addEventListener("click", function(){
alert('loaded!');
var port = chrome.runtime.connect({name: "paths"});
var spaths = document.getElementById("textarea").value;
port.postMessage({paths: spaths});
});
background.js
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
alert(msg.paths);
});
});
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
var paths = msg.paths;
chrome.storage.sync.set({key: paths}, function() {
//alert('Value is set to ' + paths);
});
});
});
在background.js中的其他位置,当我希望使用存储值时:
chrome.storage.sync.get(['key'], function(result) {
if (!result.key){alert('Value is empty')}
else {alert(result.key);
//did stuff with result.key
};
})
form.html
需要在选项卡中打开。此外,我不认为后台可以直接访问它,需要一些消息传递。弹出窗口只有在显示时才会运行,因此当它关闭时,所有内容都会立即丢失-您应该在侦听器中发送文本,以便在form.js中进行'input'
事件。