Chrome ห้องเก็บขอ sync นำเข้าข้อมูลค่า

0

คำถาม

ฉันพยายามจะเอาข้อมูลค่าของฉัน main.js (เนื้อหาภายในสคริปต์)แต่ฉันไม่ค่อยอยากจะยุ่งเกี่ finialize นี่ไม่ทางใดก็ทางหนึ่ง ฉัน maged เพื่อช่วยค่ากับหน้าต่างonload วิธีที่คุณสามารถเห็นด้านล่างในของฉัน popup.js. แต่ฉันไม่สามารถเอามันมาให้เนื้อหาสคริปต์ ฉันต้องการใช้ค่าที่เป็นตัวแปร"userInput"อยู่ในเนื้อหาภายในสคริปต์

popup.js:

function registerButtonAction(tabId, button, action) {
    // clicking button will send a message to
    // content script in the same tab as the popup
    button.addEventListener('click', () => chrome.tabs.sendMessage(tabId, { [action]: true }));
}

function setupButtons(tabId) {
    // add click actions to each 3 buttons
    registerButtonAction(tabId, document.getElementById('start-btn'), 'startSearch');
    registerButtonAction(tabId, document.getElementById('deals-btn'), 'startDeals');
    registerButtonAction(tabId, document.getElementById('stop-btn'), 'stopSearch');
}

function injectStartSearchScript() {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        // Injects JavaScript code into a page
        // chrome.tabs.executeScript(tabs[0].id, { file: 'main.js' });

        // add click handlers for buttons
        setupButtons(tabs[0].id);
    });
}

injectStartSearchScript();

window.onload = function () {
    document.getElementById('save-btn').onclick = function () {
        let valueInput = document.getElementById('deal-ipt').value;

        chrome.storage.sync.set({ 'maxBidDeal': valueInput }, function () {
            alert('Saved!');
        });
    };
};

รายการlanguage:

{
    "manifest_version": 2,
    "name": "test app",
    "description": "test desc",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": ["tabs", "<all_urls>", "storage"],
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["main.js"]
        }
    ],
    "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}

main.js:

function startSearch() {
// does soemthing
}

function deals() {
// here is my variable userInput
userInput = 
}

chrome.runtime.onMessage.addListener((message) => {
    // choose action based on received message:
    if (message.startSearch) {
        startSearch();
    } else if (message.startDeals) {
        deals();
    }
});

// sanity check: content has loaded in the tab
console.log('content loaded');

ดังนั้นฉันมั่นใจว่าฉันต้องใช้บโครเมี่ยมห้องเก็บของได้ยังไงแต่ฉันอย่าหาทางออกแน่นอน

1

คำตอบที่ดีที่สุด

1

รหัสของคุณคือโทรมา deals recursively ตลอดไปโดยไม่มีอันที่จริผ่านค่าเพราะว่าคุณไม่ได้ประกาศเป็นพารามิเตอร์และจากนั้นคุณกำลังพยายามจะใช้ userinput เกินกว่าที่ตัวแปรเป็นลำก้องปืน

คุณสามารถ promisify chrome.storage และใช้ await เหมือนนี้:

async function deals() {
  // Note that chrome.storage is already promisified in ManifestV3 since Chrome 95 
  let { MaxBidDeal } = await new Promise(resolve =>
    chrome.storage.sync.get('MaxBidDeal', resolve));

  // use MaxBidDeal right here
  console.log('MaxBidDeal', MaxBidDeal);
}
2021-10-25 19:38:39

ถ้าผมคัดลอกวาง ur รหัสเข้าไปในเนื้อหาของฉันสคริปต์ฉัไปนี้: Uncaught (in promise) TypeError: Error in invocation of storage.get(optional [string|array|object] keys, function callback): No matching signature.
exec85

อ่าขอโทษนะคุณกำลังใช้ ManifestV2 เห็นที่ปรับปรุงคำตอบ
wOxxOm

ไม่สามารถพูดขอบคุณมากพอ... มันทำงาน:-)ขอบคุณสำหรับความอดทนของคุณ! ฉันแค่ learing JS และพยายามที่จะเรียนรู้กับกา chrome วนขยายแฟ้มโครงการและนั่นทำให้ผมมากปวดหัว...
exec85

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่