DEV Community

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

Collapse
 
datamgmtdoc profile image
Joshua Girard • Edited

I get an error with unexpected token '.'
debugger:///VM90496

Attached is the screenshot...dev-to-uploads.s3.amazonaws.com/i/...
I assume you don't need a const and var for keywords? I also got an error saying keywords had been defined already. Probably user error but missing a step here...

Thanks!

Collapse
 
datamgmtdoc profile image
Joshua Girard • Edited

Total NEWB - just needed to remove the ';' from after the fetch which ended the statement...that said I got a notification that it added my keywords however when I refresh I only see it added ${keyword} to my alerts....?? Is this right: 22${keyword}%22

Collapse
 
davideladio profile image
davideladio

Hi there, im getting the same. the Javascript code works but it adds an alerts with the actual text '${keyword}' as the term to be looked up in the alert. I can't make the code work to extract the ${keyword} value and use it to set the alert :(

Anyone worked this out? Thanks so much!!!

Thread Thread
 
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!!!