[英]Passing local storage data from chrome extension to content script automatically on every page load
I wrote an chrome extension, which saves user input to the local storage via chrome.storage.sync.set/get...
and sends this data via chrome.tabs.sendMessage
to a content script, which listens via chrome.runtime.onMessage.addListener
. 我写了一个chrome扩展程序,它通过
chrome.storage.sync.set/get...
将用户输入保存到本地存储中,然后通过chrome.tabs.sendMessage
将这些数据发送到内容脚本,该内容脚本通过chrome.runtime.onMessage.addListener
进行侦听chrome.runtime.onMessage.addListener
。 This whole load and send process is triggered, when the user opens the extension per mouse click and presses a button. 当用户每次单击鼠标打开扩展并按下按钮时,就会触发整个加载和发送过程。 Before this happens the content script has not the needed data to do its task and is waiting.
在此之前,内容脚本没有执行任务所需的数据,并且正在等待。
I want, that the local storage data gets loaded (and sent to the content script) automatically on every page load without any user interaction needed. 我想让本地存储数据在每次页面加载时自动加载(并发送到内容脚本),而无需任何用户交互。
Which callback do I have to implement or where do I have to put my code in order to achieve this? 我必须实现哪个回调或必须在哪里放置代码才能实现此目的?
The whole code looks like this ( Tag and Pass are the mentioned user data): 整个代码如下所示( Tag和Pass是提到的用户数据):
function clickHandler(e) {
var tag = document.getElementById(DOM_TAG).value, pass = document.getElementById(DOM_PASS).value;
// ...query for the active tab...
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
from: 'popup',
subject: 'init',
tag: tag,
pass: pass
});
});
// Save data to local storage.
chrome.storage.sync.set({DOM_TAG: tag, DOM_PASS: pass}, function() {
// Notify that we saved.
});
}
// Once the DOM is ready...
window.addEventListener('DOMContentLoaded', function() {
// Load data from local storage.
chrome.storage.sync.get({DOM_TAG: 'defaultTag', DOM_PASS: 'defaultPass'}, function(items) {
document.getElementById(DOM_TAG).value = items.DOM_TAG;
document.getElementById(DOM_PASS).value = items.DOM_PASS;
});
document.querySelector('button').addEventListener('click', clickHandler);
});
Of course I tried to start the process inside the callback
function for DOMContentLoaded
, but that is not working since this event only gets fired when the user manually opens the extensions by clicking on it. 当然,我尝试在
DOMContentLoaded
的callback
函数中启动该过程,但这不起作用,因为仅当用户通过单击手动打开扩展名时才触发此事件。 Thats not what I want. 那不是我想要的。
I also tried to put the specific lines of code into a BackgroundScript
, with no success. 我还尝试将特定的代码行放入
BackgroundScript
,但没有成功。
You do not need to pass data from extension to content script. 您不需要将数据从扩展名传递到内容脚本。 You can access it directly in content script.
您可以直接在内容脚本中访问它。 ie:
chrome.storage.local
and chrome.storage.sync
are available to content scripts. 即:
chrome.storage.local
和chrome.storage.sync
可用于内容脚本。
Your extension's content scripts can directly access user data without the need for a background page.
您扩展程序的内容脚本可以直接访问用户数据,而无需后台页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.