Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am New to this website so I Will try my best to explain what I want.
I am Trying to make a google extension where it directly takes you to a specific Link as soon as a specific words is detected
Here is my current code:

JavaScript
replaceText(document.body);

function replaceText(element) {
  if (element.hasChildNodes()) {
    element.childNodes.forEach(replaceText);
  } else if (element.nodeType === Text.TEXT_NODE) {
    element.textContent = element.textContent.replace(/coronavirus/gi, "████");
    element.textContent = element.textContent.replace(/covid-19/gi, "████");
  }
}



As you can see it replaces the Word "Covid-19" with a blank box, But what I want is: That as soon as "Covid-19" is detected it redirects me onto a website which has already been set by me, is there a command like that?

What I have tried:

I tried many functions like:
"Redirect"
"Link"
And much more, but non of them seem to work
I am new to Java So Excuse my Lack of Understanding
Thank you in advance and I hope to get an answer very soon!
Posted
Updated 16-Jan-22 5:29am
Comments
Richard Deeming 17-Jan-22 5:50am    
NB: This kind of behaviour seems like a great way to get your extension banned as "malware".

1 solution

Try the following code to redirect as per specific word found:

<pre><script type='text/javascript'>
	function redirectURL() {
	    var specWord = getSpecificWord();
		switch(specWord) {
			case 'corona':
				window.location.replace('corona.html');
				break;
			case 'covid':
				window.location.replace('covid.html');
				break;
			case 'covid-19':
				window.location.replace('covid19.html');
				break;
			default:
				return true;
				break;
		}
		return false;//don't let the form submit
	}
	
	function getSpecificWord(Element) {
	  var specificWord = "corona";
	  return specificWord;
	}	
</script>
 
Share this answer
 
v2
Comments
Mujiboy 16-Jan-22 11:57am    
Thank You, thank you!
You don't even understand how much this helped me!
M Imran Ansari 16-Jan-22 12:00pm    
My pleasure.
Mujiboy 16-Jan-22 12:22pm    
Hey man,
I am Sorry to Disturb you again, As you know I am Very new to JavaScript, The Site works but It doesn't do it automatically, Can you give me the fields I want to change?
Example I want it to detect "Covid" and just go this Link: https://discord.com
As soon as the word is detected, I am Not Familiar with the words you wrote, Pls tell me what to replace by Saying **Replace with your word** **Replace with your Link**, I am Sorry, you don't have to do it all over again for me, As you have been way too kind to me, Good sir, I will Figure a way out, if it bothers you
M Imran Ansari 16-Jan-22 13:20pm    
What do you mean by automatically? and I refer the location where you can put your own logic to trace your specific word, on the basis you want to triggered.
Mujiboy 16-Jan-22 14:05pm    
You have done it now, Thanks for reediting Original, Turns out I was putting Link in the Wrong Place, Now, Iam Sorry To have Distrubed you, And You Will Forever be Remembered in my heart, Mr Imran Ansari
-From A heartbroken programmer Student

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900