繁体   English   中英

如何在chrome扩展中使用localStorage

[英]How to use localStorage in chrome extension

我创建了 chrome 扩展,当点击扩展图标时它会打开相机。 我在 content.js 中的代码运行良好。 但是当我重新加载站点相机时,它就消失了。 我知道如果我想显示相机甚至重新加载页面,我必须使用 localStorage。 但是当我尝试使用它时,我的相机不工作。 你能帮我吗? 你能告诉我如何在这种情况下使用 localStorage 吗?

这是我在content.js 中的代码

chrome.runtime.onMessage.addListener(
    function({ShowCamera}, sender, sendResponse) {
        if(ShowCamera){
            let html = `
            <div class = "video-container">        
              <video style="width: 240px; margin: 0px;" autoplay="true" id="videoElement"></div>
            </div>`

            function setupCam() {
                 navigator.mediaDevices.getUserMedia({
                   video: true
                }).then(mediaStream => {
                    document.querySelector('#videoElement').srcObject = mediaStream;
                }).catch((error) => {
                  console.warn(error);
                });
              }

              setupCam();
              document.body.innerHTML += html;
            }   
 });

背景.js

chrome.browserAction.onClicked.addListener(function(){
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {ShowCamera: "true"});    
  });
})
chrome.storage.local.set({ myKeyForCamAccess: 'myValueHere' });

chrome.storage.local.get('myKeyForCamAccess', (data) => {
        if (data.myKeyForCamAccess) {
          //do somthing
        }
      });

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM