I want to create a Chrome Extension that works with Facebook. I took random text from https://randomtextgenerator.com/ (first paragraph) and now I want to use it in my source.js to create the extension.I was told that I should send HTTP request to NodeJs to take this text and use, who knows how I can do?
NodeJS / index.js
const PORT=8888;
const axios=require("axios");
const cheerio=require("cheerio");
const express=require("express");
const {response}=require("express");
const app=express();
const url="https://randomtextgenerator.com/";
axios(url)
.then(response=>{
const html=response.data
const $=cheerio.load(html);
const articles=[];
$("#randomtext_box", html).each(function () {
const title=$(this).text()
articles.push(
title
)
})
let articles1=articles.join();
let y;
for (let i=0; i < articles1.length; i++) {
if (articles1[i] == ".") {
if (articles1[i + 1] == " ") {
if (articles1[i+2] == "\n") {
y=i;
break;
}
}
}
}
let newArticles = articles1.substring(0,y+1);
console.log(newArticles);
}).catch(err=>console.log(err));
app.listen(PORT, ()=>console.log(`server runing on ${PORT}`));
Chrome Extension / source.js
function(newArticles){
let text= `<div class="text1"></div>`
document.body.appendChild(text);
text.innerText=newArticles;
}
I tried it with module.exports, but the error "ERROR,document is not defined".