0

Connect from popup.html to background.js script in Chrome Extension. When I click on the button, I want the background script to open a new tab.

Background Script:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {    
    if (message == "output") {

       var win = window.open('https://www.google.ca/webhp?hl=en&sa=X&ved=0ahUKEwjesaKd26TcAhWNw4MKHcN-CwgQPAgD', '_blank');
       win.focus();    
    }    
});

HTML:

<body>
    <script src="next.js"></script> 
    <input id="output" type="button" value="Go To Alternative Forum"/>
</body> 

Next:

 document.getElementById("output").addEventListener(("click"),function(){

        chrome.runtime.sendMessage("output"); 
});
3
  • Can you clarify your question and code in the post body? Consider using a code snippet. Commented Jul 18, 2018 at 21:39
  • my bad. its my first time. just did. and its regarding a chrome popup.html extension. Commented Jul 18, 2018 at 21:59
  • Shouldn't the script have src="" instead of href=""? Commented Jul 18, 2018 at 22:38

2 Answers 2

2

You can do that by window.open.

But if you want to do connect your html to background.js, then the official way is to send the message from your script next.js to the background.js and then background.js will create new tab to open the link.

Following code will work in your case:

next.js

 document.getElementById("next").addEventListener("click",function(){

        chrome.runtime.sendMessage({type: "openTab",value:code},function(response) {

        });

    });

popup.html

<body>
    <script src="next.js"></script>
    <input id="next" type="button" value="Go to google" />
</body>

background.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {

    if (request.type == "openTab") {

        chrome.tabs.create({"url": "http://www.google.com", "selected": true}, function (tab) {

        });

    }

});
Sign up to request clarification or add additional context in comments.

3 Comments

this code makes alot more sense. however, i am unable to connect my html to the next.js. the addEventListener dosen't connect. but thank you
I apologize. There was an error in eventlistener line. I've updated the code. It should work now.
i feel it nessary to imply that the next file is not the content script. do i need to add anything to the manifest.json.
1

Your script reference in your code should have a src="" attribute instead of a href="". Example here:

HTML:

<body>
    <script src="next.js"></script>
    <input onclick="start();" type="button" value="Go to google" />
</body>

next.js:

function start() {
    var win = window.open('https://www.google.ca/webhp?hl=en&sa=X&ved=0ahUKEwjesaKd26TcAhWNw4MKHcN-//CwgQPAgD', '_blank');
    win.focus();
}

1 Comment

thanks that helps. but i dont think the popup.html can connect to the background script. due to the fact its a chrome cast. thanks tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.