[英]How can I sync localStorage across Chrome instances (or use chrome.storage.sync without a published extension)?
[英]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.