0

I am building a chrome extension that opens a search bar upon pressing a hotkey and when enter is pressed it shows dummy results.

I have defined the following files

background.js

chrome.commands.onCommand.addListener((command) => {
    if(command === "launch") {
        chrome.tabs.executeScript({
            file: "insertSearchBar.js"
        });
    }
});

This file launches insertSearchBar.js correctly.

insertSearchBar.js

var div=document.createElement("div");
var innerDiv=document.createElement("input");
...
div.addEventListener('keypress', function(e) {
    if(e.key == "Enter") {
        chrome.tabs.executeScript({
            file: "insertSearchResultView.js"
        });
    }
})

This file doesn't execute chrome.tabs.executeScript correctly and doesn't launch a new view on the current tab.

insertSearchResultView.js

var div=document.createElement("div");
document.body.appendChild(div);
var innerDiv=document.createElement("input");
div.appendChild(innerDiv);
  • Is this a manifest version 2 or 3 Chrome extension? – Yogi Feb 11 '23 at 00:17
  • You are using `chrome.tabs.executeScript` incorrectly. Check for errors in DevTools. – Norio Yamamoto Feb 11 '23 at 00:20
  • Note that the popup is a separate window so it has its own separate devtools: right-click inside the popup and select "inspect" in the menu. – wOxxOm Feb 11 '23 at 00:44
  • Its version 2 @Yogi – Avi Kumar Singh Feb 11 '23 at 11:40
  • 1
    Your insertSearchBar.js is injected so it becomes a content script and hence it can't use `chrome.tabs` API. If you want to add the script dynamically you can use the dynamic `import()` as shown [here](/a/53033388) or simply put the code in insertSearchBar.js instead of using a separate file. On the other hand, it's unclear what your goal is, e.g. do you want to modify the web page or the extension's popup page, so your current code may be entirely wrong. – wOxxOm Feb 11 '23 at 12:19

0 Answers0