I will start with a clearer definition of each page / script type.
First it is background page/script . background script is where your extension resides. This is not required, but in order to do most of the expansion things, you need it. In it, you can configure various event listeners and, depending on what you want. He lives in his own small world and can only interact with other pages and scripts using chrome.* Apis. If you configured it as an event page, it works the same way, except that it is unloaded when not in use, and loaded back into memory when it needs to do something.
Content scripts refer to the entered Javascript and / or css. They are the main tool used to interact with web pages. They have very limited access to chrome.* Apis, but they have full access to the DOM of the page to which they are entered. We will return to their use in a minute.
Now for Popup pages . Unlike background script and content script , pop-ups have both part of HTML and JS . The HTML part is like any other page, just small and like a pop-up overlay coming out of an icon. However, the script part can do everything the background page does, except that it is unloaded whenever the popup closes.
Now that the differences are clearer, let's move on to what you want to do. It looks like you want to open a popup so that the user enters search text on the current tab, and then selects that text on the page. Since you said that you already know how you plan to highlight the text, I will leave this part to you.
First configure the manifest file. For this particular action, we do not need a background script. We need only the permissions "tabs" and "activeTab" . This will allow us to enter our script later. We also need to determine the action of the browser with its pop-up window. In general, it would look something like this:
"browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "tabs", "activeTab" ]
Now in our popup.html file we can only have markup and css, without inline code. We will put all this in our JS file and include it. Something like this should work:
<!DOCTYPE html> <html> <head> <script src="popup.js"></script> </head> <body> <input type="text" id="searchText"> <button id="searchButton">Search</button> </body> </html>
Here we return to the contents of the script. There are two ways to insert script content, first define it in the manifest. This works best when you always want to enter it for a specific set of URLs. Secondly, use the chrome.tabs.executeScript method to enter it when we need to. This is what we will use.
window.onload = function(){ document.getElementById('searchButton').onclick = searchText; }; function searchText(){ var search = document.getElementById('searchText').value; if(search){ chrome.tabs.query({active:true,currentWindow:true},function(tabs){ chrome.tabs.executeScript(tabs[0].id,{file:search.js}); chrome.tabs.sendMessage(tabs[0].id,{method:'search',searchText:search}); }); } }
In doing so, we successfully entered our script, and then sent the search text to the script. Just make sure the script is wrapped in the onMessage as follows:
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
And that pretty much sums it up. With this, you can make it work. If something else is unclear, let me know and I will fix it.