I
I
Igor2016-09-08 08:18:20
JavaScript
Igor, 2016-09-08 08:18:20

How to send object from background page to google extension?

//манифест
{
  "manifest_version": 2,
  "name": "Help Panda",
  "version": "2.0",
  
  "icons": {
    "128": "panda.png"
  },
  "content_scripts": [
    {
      "matches": [ "*://*/*" ],
      "js": [ "content.js" ]
    }
  ],

  "background": {
    "scripts": ["background.js"]
  },
      "browser_action": {
        "default_title": "Help Panda",
        "default_icon": "panda.png",
        "default_popup": "popup.html"
    }
  
}

//content.js
chrome.extension.sendMessage({objChromMes:localNumb});

//popup.js
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
var a = request.objChromMes; // данные о сайте
 // данные о проведенном времени
// тут делаем с этими данными что хотим.
menu.innerHTML =a;
  });

<!doctype html>
<html>
  <head>
    <title>Потерянное время LostTime</title>
  <link href="css.css" rel="stylesheet" type="text/css"/> 
  </head>
  <body>
  <div id="options"><!-- меню -->
<h3>first app</h3>
</div>
<div id="dannie"></div> <!-- в этот блок буду загружать данные, которые будут показываться пользователю-->
    <script src="popup.js"></script><!-- скрипт, выполняющийся при нажатии на иконку расширения-->
  </body>
</html>

Why does not it work?
How to correctly send an object from the site to the chrome extension.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-09-11
@IgorBee

/// content.js
calc=ваш объект или массив,что угодно.
chrome.runtime.sendMessage({greeting:calc});

background.js
  chrome.runtime.onMessage.addListener(

   function(request, sender, sendResponse) {

   localStorage['abbrev'] += request.greeting;

  });

poupop.js

chrome.storage.local.get('channels', function (result) {
        channels = result.channels;
       
        menu.innerHTML +=channels;
    });

1. We send a message to the background js.
2.Background Js already has write access to the extension's local storage.
3.From the popup we read the record in the local storage.
Shl, you can not directly save the content to local storage and read it in the extension.

P
Pianist, 2016-09-08
@kzakhariy

How to send object from background page to google extension?

The question is not correctly composed, as I understand it, you need to send a message from the site to the background of the running application
{
  "name": "App",
  "description": "Desc",
  "version": "1.1",
  "icons": {
    "16": "icon-16.png",
    "128": "icon-128.png"
  },
  "browser_action": {
    "default_icon": "icon-16.png",
    "default_popup": "popup/popup.html"
  },
  "background": {
    "scripts": [
      "js/listener.js"
      "js/background.js",
    ]
  },
  "externally_connectable": {
    "matches": [
       "*://*.site.com/*",
    ]
  },
  "manifest_version": 2,
  "minimum_chrome_version": "35",
  "permissions": [
    "webNavigation",
    "notifications",
    "tabs",
  ]
}

//Listener.js
//Listen to messages and return a response
chrome.runtime.onMessageExternal.addListener(function (message, sender, sendResponse) {
    console.log(message);
    console.log(message.type);
    if(message.type == 'checkBrowserAppInstalled'){
        sendResponse({
            installed: true
        })
    }
    return true;
});

//site.com
//Send a message from the site to the application
var BROWSERAPPID = 'ID OF APP';
var OBJECT_TO_SEND = {type: "checkBrowserAppInstalled", data : [1,2,3]};
var checkApp = function () {
                    return new Promise(function (resolve, reject) {
                        chrome.runtime.sendMessage(BROWSERAPPID, OBJECT_TO_SEND , null, function (response) {
                            if (response && response.installed === true) {
                                resolve(response);
                            } else {
                                reject({reason: "APP_NOT_FOUND"})
                            }
                        });
                    });
                }
checkApp().then(function(){
    ...
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question