Javascript-форум (https://javascript.ru/forum/)
-   Javascript под браузер (https://javascript.ru/forum/css-html/)
-   -   Как передать URL активной вкладки в popup (https://javascript.ru/forum/css-html/64649-kak-peredat-url-aktivnojj-vkladki-v-popup.html)

Areaccely 27.08.2016 08:27

Как передать URL активной вкладки в popup
 
Доброе утро!
Хочу передать URL активной вкладки из content script.js в popup.html
Помогите!

Areaccely 13.09.2016 22:57

Ура! Нашел решение.
content_scripts.js
var urlTab;
urlTab = window.location.href;
//Отправка информации в popup.js
chrome.runtime.sendMessage({greeting: "hello", data:urlTab }, function(response) {
  console.log(response.farewell);
});


popup.js
document.addEventListener("click", function() {
	chrome.tabs.executeScript(null, {
		file: "/content_scripts/content_scripts.js"
	});
});

//вставка URL из браузера 
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello"){
		var input = document.getElementById("Url");
		input.value = request.data;
		sendResponse({farewell: "goodbye"});
	};
  });


Часовой пояс GMT +3, время: 14:14.