I get the title in the content script or in the background, but how can I send it or get it in a popup window that opens with my own script - can I not get the title of the page in it (do I need the h1 of the page on which this popup extension is opened)?

UPD: In the popup window, I want to transfer the title to the server, but in the same script I cannot get it with the standard script, I also undefined in localStorage, although on the whole page I saved the title in localStorage by another script (which is content.js) .

The manifest device is as follows:

 {
     "manifest_version": 2,
     "version": "0.1",
     "name": "Name",
     "description": "Description",
     "content_scripts": [
         {
             "matches": ["*: // * / *"],
             "css": ["ctyle.css"],
             "js": ["content.js"],
             "run_at": "document_end"
         }
     ],
     "background": {
     "scripts": ["background.js"]
   },
     "icons": {
         "16": "icon-16.png",
         "48": "icon-48.png",
         "128": "icon-128.png"
     },
     "permissions": [
         "tabs",
         "desired site / *",
         "storage"
   ],
     "browser_action": {
         "default_title": "Name",
         "default_icon": "icon-32.png",
         "default_popup": "popup.html"
     }
 }
  • Please clarify what the problem is, or provide additional information to understand exactly what the question is. In the current form, it is almost impossible to understand exactly what you are asking. Visit the “ how to ask questions ” page to understand how to write questions. - VenZell
  • Clarified, now can be clearer. - Timur Musharapov
  • If you are given an exhaustive answer, vote for it and mark it as correct (click on the check mark next to the selected answer). - VenZell

1 answer 1

The content on the page and the extensions are different localStorage , so you can not get the value. However, you do not need it.

Using the sendMessage() method, you can send a command for content.js from popup.js , and in content.js read this command using onMessage() and perform certain actions on the page content, returning the result back to popup.js .

Since you have a confusion with what you need to get ( h1 or title ), I give an example to get a title that can be easily corrected for your needs.

manifest.json

 { "manifest_version": 2, "name": "Demo extension", "description": "Playground for the stackoverflow needs", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "activeTab", "https://www.google.ru/*" ], "content_scripts": [{ "matches": ["*://*/*"], "js": ["content.js"], "run_at": "document_end" }] } 

popup.html

 <!doctype html> <html> <head> <title>Demo</title> <script src="popup.js"></script> </head> <body> <form> <input id="title" name="title" /> </form> </body> </html> 

popup.js

 // Заносим значение title в input function setTitleInfo(title) { document.getElementById('title').value = title; } // Получение текущей вкладки function getCurrentTab() { return new Promise(function (resolve, reject) { chrome.tabs.query({ active: true, // Выбор активной вкладки currentWindow: true }, function (tabs) { resolve(tabs[0]); }); }); } // Отправка запроса к content.js после получения текущей вкладки getCurrentTab().then(function (tab) { var currentTabId = tab.id; chrome.tabs.sendMessage(currentTabId, { action: 'TitleInfo' // Уникальное имя для нашего события }, setTitleInfo); // Функция setTitleInfo будет выполнена после ответа content.js }); 

content.js

 // Подписка на события chrome.runtime.onMessage.addListener(function (msg, sender, response) { // Ждем события с определенным именем if ((msg.action === 'TitleInfo')) { // Возвращаем результат вычислений response(parseTitle()); } }); function parseTitle() { var title = document.querySelector('title'); if (title) { return title.textContent; } return false; } 
  • Thank you very much, you really helped! - Timur Musharapov
  • VenZell, by the way, the title is returned as HTMLInputElement, and how to get the string (plain text)? - Timur Musharapov
  • @TimurMusharapov, then title.value . - VenZell
  • var title = document.getElementsByTagName ("h1") [0]; if (title) {return title.value; } - returns "undefined", even if h1 is. - Timur Musharapov
  • @TimurMusharapov var title = document.querySelector("h1"); if (title) { return title.textContent; } var title = document.querySelector("h1"); if (title) { return title.textContent; } var title = document.querySelector("h1"); if (title) { return title.textContent; } - VenZell