简体   繁体   English

每次页面加载时自动将本地存储数据从chrome扩展名传递到内容脚本

[英]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. 在此之前,内容脚本没有执行任务所需的数据,并且正在等待。

Concrete requirements / questions: 具体要求/问题:

  • 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): 整个代码如下所示( TagPass是提到的用户数据):

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);
});

What I already tried: 我已经尝试过的

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. 当然,我尝试在DOMContentLoadedcallback函数中启动该过程,但这不起作用,因为仅当用户通过单击手动打开扩展名时才触发此事件。 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.localchrome.storage.sync可用于内容脚本。

Your extension's content scripts can directly access user data without the need for a background page. 您扩展程序的内容脚本可以直接访问用户数据,而无需后台页面。

storage docs storage文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Chrome扩展程序编程中的内容脚本访问本地存储 - Access local storage from content script in Chrome extension programing 如何从内容脚本访问 chrome 扩展本地存储数据? - How to access chrome extension local storage data from content scripts? 获取本地存储值到内容脚本[Chrome扩展程序]的简便方法 - Easy way to get Local Storage Value to Content Script [Chrome Extension] Chrome扩展程序:内容脚本无法将本地HTML文件加载到iframe中 - Chrome Extension: content script is unable to load local HTML file into iframe 将 Chrome 扩展数据从 Chrome 本地存储迁移到 MongoDB - Migrating Chrome Extension Data from Chrome Local Storage to MongoDB 使用Chrome扩展程序内容脚本将iframe加载到页面中 - Load iframe into the page, using Chrome Extension content script 将消息从内容脚本传递到基于Angular JS的Chrome扩展 - Passing a message from content script to Angular JS based Chrome Extension Chrome 扩展 - 从弹出窗口到内容脚本的消息传递 - Chrome Extension - Message Passing from Popup to Content Script 如何从Chrome扩展程序本地存储中获取数据? - How to get data from chrome extension local storage? 从Chrome扩展程序获取每个页面上的网址和内容 - Getting url and content on EVERY page from Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM