铬储存同步的输入值

0

的问题

我试着得到输入的价值在我main.js (脚本内容),但是我努力finialize它以某种方式。 我马吉德保存价值的窗口。加载的方法,因为可以看到下面我的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!');
        });
    };
};

清单。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 递归永远没有实际传递的价值,因为你没有宣布一个参数,然后你试图使用 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

不能说声谢谢。 它的工作原理:-)感谢您的耐心! 我只是学习JS和尝试了解与铬扩展项目和这给了我这么多的头痛...
exec85

其他语言

此页面有其他语言版本

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