Forum

Chrome Extension, M...
 
Notifications
Clear all

Chrome Extension, Manifest V3, from POPUP to CONTENT message

1 Posty
1 Users
0 Reactions
450 Widok
0
Topic starter

example 

1 Answer
0
Topic starter

manifest.json

{
    "name": "My extension",
    "version": "0.0.5",
    "manifest_version": 3,
	"description": "my description",
	"icons": {
		"64": "icon-64.png"
	},
    "action": {
		"default_icon": {
			"64": "icon-64.png"
		},
        "default_title": "Asd Title",
        "default_popup": "popup.html"
    },
	"background": {
        "service_worker": "background.js"
    },
	"content_scripts": [
		{
		   "matches": ["<all_urls>"],
			"js": ["content.js"],
			"run_at": "document_end",
			"all_frames": true
		}
	],
	 "content_security_policy": {
	   "extension_pages": "script-src 'self'; object-src 'self'"
	},
	"permissions": [
		"tabs",
		"management",
		"activeTab",
		"management",
		"browsingData",
		"debugger",
		"background",
		"tabs",
		"unlimitedStorage",
		"scripting",
		"storage"
	],
}

popup.html

<!DOCTYPE html>
<html>
<head>
	 
</head>
<body>
 
<button id="test_btn" style="width:100px">test btn</button>
<input type="text" id="test"  name="test">

 http://popup.js 

</body>
</html>


popup.js

document.addEventListener('DOMContentLoaded', function() {
	let regexBtn = document.getElementById('test_btn')
	regexBtn.addEventListener('click', testAction)
});


function testAction(){
		chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
		{
			chrome.tabs.sendMessage(
			tabs[0].id, 
			{ 
			   some_data: "test_something", 
			},function (response){});
		});
}

content.js

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) =>
{
	if (msg.text && (msg.text == "test_something")) 
	{
		console.log("TEST OK");
	}
	
});

Odpowiedź

Author Name

Author Email

Your question *

 
Preview 0 Revisions Saved
Share: