DEV Community

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

Corentin on August 27, 2019

Google Alerts is a very handy and powerful service to stay informed of what's happening in a particular field. I'm a huge fan of this service and...
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!!!

Collapse
 
kremzeek profile image
Michael Lệ Moore

I got as far as being able to create a Google alert. However, instead of creating separate alerts for every search term assigned to var 'keywords', it creates one alert comprised of each search term separated by commas, i.e. one concatenated search string. Can anyone assist? TIA.

Collapse
 
kremzeek profile image
Michael Lệ Moore

I figured out the issue. I was using davideladio's fix but accidentally declared the param variable with the keywords variable instead of the keyword constant. Deleting the 's' fixed the issue!

Collapse
 
jojohairdog profile image
HHBO

What you added worked very well!
I have a ridiculous problem.
Id like to use quotation marks in my keywords and darn it- can't work it.
The regular escape character for quotation marks don't work.
var keywords=[" \"Topic One\" " , " \"Topic Two\" "]

and it gives a 404 error here-
fetch("google.com/alerts/create?x=AMJHsmW...", {
blaah blah}

Thank you!

Thread Thread
 
thomasc profile image
Thomas

I am having the same issue without using quotation marks in my keywords. Were you able to fix this?

Collapse
 
bergeny profile image
bergeny

There's a bug in the script
everytime I run it it shows the following error on this line :
.then(() => { setTimeout(function() {addAlert(i+1)}, (Math.min(i || 2, 30) * 1000)) })

Uncaught SyntaxError: Unexpected token '.'

Collapse
 
bergeny profile image
bergeny

After a trial I found the error is the ";" just before the ".then" line
You just need to remove it
Regards

Collapse
 
paullchaffey profile image
paull chaffey

Hello, anyone have a script to add google alerts in bulk?

Collapse
 
davideladio profile image
davideladio

check it out

Collapse
 
thomasc profile image
Thomas

Trying this in 2023 and getting 404 errors when attempting to run the fetch() code. Anyone have ideas?