DEV Community

Discussion on: How to create a bunch of Google Alerts, in 3 minutes?

 
davideladio profile image
davideladio

Fixed!!! The issue is that having the 'params' inside an string like this:

"params=hexcode${keyword}hexcode" makes the interpreter to not extract the value for ${keyword}

Instead of that in your code define 2 new var like this:
var left = "params=hexcode%22"

This var has 'all the text on the left of the actual keyword you want to add'

var right = "%22hexcode"

This var has 'all the text on the left of the actual keyword you want to add'

Now in addAlert(i) build a new params var containing the term you want to add in your google alert like this:

function addAlert(i) {
// Retrieve the keyword to work with
const keyword = encodeURIComponent(keywords[i])

//build the params var with the term to be added
var params = left + keyword + right

And all that's left is to use it (without any '$') in your fetch getting something like this:

"referrer": "google.es/alerts",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": params,
"method": "POST",
"mode": "cors",
"credentials": "include"

I hope this will help anyone!!!