0

I built a Chrome extension using angular. But I encourted an issue in regard to send data from contentscript to popup. any tips for sending data from input tag to display in popup? Thanks

Japy
  • 31
  • 6
  • Use messaging or executeScript, [more info](https://stackoverflow.com/q/4532236). If you need further assistance, add an [MCVE](/help/mcve) to the question. – wOxxOm Jun 20 '22 at 11:45

1 Answers1

0

Follow these steps:

  1. First, send the message from contentScript file

    var type = e.detail.type; // a "key" for your data
    var data = e.detail.data;
    
    chrome.runtime.sendMessage({type: type, data: data});
    
  2. Now you can get message in your Angular app

    /// <reference types="chrome"/>
    
    if (chrome.runtime) {
        chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
            const type = message.type;
            const data = message.data;
    
            return new Promise((resolve, reject) => {
                // Write your codes
            });
        });
    }
    

    Be sure to add the following code to the top of your TS or JS file

    <reference types="chrome"/>
    

Note: If chrome.runtime has as error, then you can install this npm package to fix it

npm install @types/chrome --save-dev
Kamran Taghaddos
  • 452
  • 1
  • 10
  • 22