T
T
Topsky2016-08-03 22:36:35
Google Chrome
Topsky, 2016-08-03 22:36:35

How to work with chrome.storage.sync.(get\set)?

Dear, I need help, I can't figure out how to work with chrome.storage.sync via content-script...

{
    "name": "PTS",
    "version": "1.0",
    "manifest_version": 2,
    "content_scripts": 
    [
        {
            "matches": ["https://google.ru/"],
            "js": ["jquery.js", "include.js"]
        }
    ],
    "background": {
           "scripts": ["window.js"]
         },
    "browser_action": 
    {
        "default_popup": "window.html",
        "default_title": "PTS"
    },
    "permissions":
    [
        "storage",
        "notifications" 
    ]
}

The essence of the extension, just updating the page at intervals.
I can’t make a settings window, not through options.html, but through my own popup.html (window.html - in my case)
-- This is Window.js, this code is controlled from window.html, just a popup, with a text box , and the save button.
$().ready(function()
{
  chrome.storage.sync.get('interval', function (result) 
  {
   		$('body > form > input[type="text"]').val(result.interval)
    });

  
  $("body > form > button").click(function()
  {
    chrome.storage.sync.set({"interval" : $('body > form > input[type="text"]').val()})
    chrome.notifications.create({type: 'basic', iconUrl: 'icon.jpg', title: "", message: "\nИнтервал успешно сохранен." }, function(){window.close();})
  })

})

This is include.js - content script
$().ready(function()
{
  alert("Loaded...");

        var INTERVAL = 600000;

  setInterval(function()
  {
    	location.reload();
  }, INTERVAL)
})

The question is, how can I get data from window.js (which has access to the chrome.storage (api) storage
, into include.js (which does not have direct access to api)
I need to write and read data in window.js for settings (interval )
And get this data from window.js, i.e. read (interval) from chrome.storage.sync

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2016-08-03
@Topsky

Only via messages
https://developer.chrome.com/extensions/messaging

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question