Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:11:43+00:00 2026-06-17T01:11:43+00:00

I would like to make a chrome extension that use a popup to do

  • 0

I would like to make a chrome extension that use a popup to do :

  1. select text
  2. click on the chrome extension icon
  3. get it in the popup (textarea, …)

This question was already asked here but Google did updates and the code I found is not working anymore …

selection.js

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.method == "getSelection")
      sendResponse({data: window.getSelection().toString()});
    else
      sendResponse({}); // snub them.
});

popup.html

    <!DOCTYPE html> 
<html>
<head>
<style>
  body { width: 300px; }
  textarea { width: 250px; height: 100px;}
</style>
<script>


function pasteSelection() {
  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function (response) {
      var text = document.getElementById('text'); 
      text.innerHTML = response.data;
    });
  });
}

function getSelectedText(){
   if (window.getSelection){
      var str = window.getSelection();
   }else if (document.getSelection){
      var str = document.getSelection();
   }else {
      var str = document.selection.createRange().text;
   }
   return str;
}

function affichage(){
var sel = getSelectedText();
alert(sel);
}

function addtext() {
    document.form.champ.value = getSelectedText();
}

</script>
</head>
<body>
<form>
<textarea id="text"></textarea>
<button onclick="pasteSelection(); " type="submit">get text</button>
</form>
</body>
</html>

manifest.json

 {
 "name": "Selected Text",
 "version": "0.1",
 "description": "Selected Text",
 "options_page": "page_options.html",
 "browser_action": {
   "default_title": "Selected Text",
   "default_icon": "icon.png",
   "default_popup": "popup.html" 
 },
 "permissions": [
   "tabs",
   "chrome://favicon/",
   "http://*/*", 
   "https://*/*"
 ],
 "content_scripts": [
  {
    "matches": ["http://*/*"],
    "js": ["selection.js"],
    "run_at": "document_start",
    "all_frames": true
  }
 ],
 "manifest_version": 2
}

I thank you in advance for your help 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:11:45+00:00Added an answer on June 17, 2026 at 1:11 am

    There are multiple problems in your script

    • chrome.extension.onRequest is deprecated in favor of chrome.extension.onMessage
    • chrome.tabs.sendRequest is deprecated in favor of chrome.tabs.sendMessage
    • CSP will not allow inline scripting and <script> tag in html code.
    • window object of Content Script is different from normal page window object.

    After applying multiple changes to code i got it working

    manifest.json

    Eliminated not applicable sections of manifest

    {
     "name": "Selected Text",
     "version": "0.1",
     "description": "Selected Text",
     "browser_action": {
       "default_title": "Selected Text",
       "default_popup": "popup.html" 
     },
     "permissions": [
       "tabs",
       "http://*/*", 
       "https://*/*"
     ],
     "content_scripts": [
      {
        "matches": ["<all_urls>"],
        "js": ["selection.js"],
        "run_at": "document_start",
        "all_frames": true
      }
     ],
     "manifest_version": 2
    }
    

    popup.html

    Ensured popup.html adheres to CSP

    <!DOCTYPE html>
    <html>
    
        <head>
            <style>
                body {
                    width: 300px;
                }
                textarea {
                    width: 250px;
                    height: 100px;
                }
            </style>
            <script src="popup.js"></script>
        </head>
    
        <body>
            <textarea id="text"></textarea>
            <button id="submit">get text</button>
        </body>
    
    </html>
    

    popup.js

    Script to pick current tab and send message and update DOM.

    function pasteSelection() {
        //Select current tab to send message
        chrome.tabs.query({
            "active": true,
            "currentWindow": true,
            "status": "complete",
            "windowType": "normal"
        }, function (tabs) {
            //It returns array so looping over tabs result
            for (tab in tabs) {
    
                //Send Message to a tab
                chrome.tabs.sendMessage(tabs[tab].id, {
                    method: "getSelection"
                });
            }
        });
    }
    //Adding a handler when message is recieved from content scripts
    chrome.extension.onMessage.addListener(function (response, sender) {
    
        //Set text to text area
        var text = document.getElementById('text');
        text.value = response.data;
    });
    
    // Bind On click event to pasteSelection() function
    document.addEventListener("DOMContentLoaded", function () {
    
        document.getElementById("submit").onclick = pasteSelection;
    });
    

    selection.js

    Passes selected text to popup.html

     //Add a handler to handle message sent from popup.html
     chrome.extension.onMessage.addListener(function (request, sender) {
         //Hanlde request based on method
         if (request.method == "getSelection")
         //Send selected text back to popup.html
         chrome.extension.sendMessage({
             data: document.getSelection().toString()
         });
         else chrome.extension.sendMessage({}); // snub them.
     });
    

    References

    • tabs.query()
    • tabs.sendMessage()
    • extension.onMessage()
    • extension.sendMessage()
    • CSP
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a Chrome extension that provides a new object inside
I would like to create a google chrome extension. Specifically, I'd like to make
I'm creating an extension for Chrome (Just noraml HTML/JS). I would like to make
I'd like to create a chrome extension to make requests for page resources that
I would like make an extension method for the generic class A which takes
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a function that takes in a list of integers
I would like to make register button, but when people click on it. A
I'm developing a Google Chrome extension that makes heavy use of the context menu,
I would like to make an overflow menu similar to the chrome app in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.