क्रोम भंडारण सिंक इनपुट मूल्य

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

manifest.json:

{
    "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

अगर मैं कॉपी पेस्ट उर कोड में सामग्री स्क्रिप्ट मैं इस मिल: Uncaught (in promise) TypeError: Error in invocation of storage.get(optional [string|array|object] keys, function callback): No matching signature.
exec85

आह, क्षमा करें, आप का उपयोग कर रहे हैं ManifestV2, देखें अद्यतन का जवाब.
wOxxOm

नहीं कर सकते हैं, धन्यवाद कहने के लिए पर्याप्त.... यह काम करता है :-) आपके धैर्य के लिए धन्यवाद! मैं कर रहा हूँ बस learing जे एस और जानने की कोशिश के साथ एक क्रोम एक्सटेंशन परियोजना और है कि मुझे इतना दर्द...
exec85

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में