I
I
Ilya T2019-03-24 01:20:23
Google Chrome
Ilya T, 2019-03-24 01:20:23

How to get HTML object on click in extension via chrome.storage?

I'm trying to get the saved object in chrome.storage, by clicking in the extension itself, but it turns out null. Moreover, if I get it immediately, then everything is OK, but if by clicking from the extension, then null comes out. I'd be grateful if anyone could tell me what's wrong.
Manifesto

{
      "manifest_version": 2,

      "name": "google extension",
      "version": "0.1.0",
      "description": "google extension",
      "permissions": [
              "storage"
            ],
       "browser_action": {
        "default_popup": "popup.html" //код самого виджета будет лежать тут
      },
      
      "background":{
        "scripts":["background.js"]
    },
      "icons": {},
      "content_scripts": [{
      "js": ["popup.js"],
      "matches": ["http://exemple.com/*"]
      
    }]

    }

popup.html - the Html extension itself
<!doctype html>
    <html>
    <head>
        <title>Test1</title>
    </head>
    <body>
    <h1>Action:</h1>
    <button id="click">Add To Friend List</button>
    <script src="popup.js"></script>
    </body>
    </html>

popup.js
var el = document.getElementById('click');//отслеживаем клик в расширении

    var value = document.getElementById('errr');//объект на странице 

    chrome.storage.sync.set({key: value}, function() {
         console.log('Value is set to ' + value);
       }); //записываем в chrome.storage
      
    if (el){//если кликнули в нашем расширении
    	el.onclick = function() {
    	    chrome.storage.sync.get(['key'], function(result) {
    	      alert('Value currently is ' + result.key);
    	    });//получаем null хотя должны получить объект HTML
    	    
    	  };
    	
    }

the page where I run the extension (code):
<!DOCTYPE HTML>
<html>
 <head>
  <meta charset="utf-8">
  <title>Тег BUTTON</title>
 </head>
 <body>
  <p   style="text-align: center"><button  id = "errr" onclick="window.location='http://www.google.com'">Click Me</button> </p>
 </body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question