简体   繁体   中英

Send Data from chrome extension to Node.js

I have created a simple Chrome extension which stores all a few clicks on some Z webpage in my content_script. So, I want to know a way to send this data (json) to my node.js file (lets say /start).

How exactly to go about this? starting from sending data from the extension through ajax AND receiving this data in my /start router js file?

Any help appreciated. Thanks.

I found a solution to my problem, if anyone falls into the same hurdle.

Basically, I sent the data from my content_script (js file) to background (js file) of my extension through chrome runtime message. Now Background file must be defined in the manifest, and is basically a file which runs in the background. From Here, I made a Web API call (XMLHttpRequest) from the background.js to my localhost server AND made it a POST function.

var xj = new XMLHttpRequest();
xj.open("POST", "http://localhost:3000/log", true);
xj.setRequestHeader("Content-Type", "application/json");
xj.send(JSON.stringify({ action: <your data>}));
xj.onreadystatechange = function () { if (xj.readyState == 4) { console.log(xj.responseText); } }

Now, in my node.js index page, I capture the send request in a Post function

router.post('/log', function(req, res, next){
     console.log(req.body) //Your data from the extension
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM