Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I am Making a Chrome Extension, which can redirect you to "Youtube.com" when the word "Covid" is detected on the webpage, Example you type "Covid" on the search bar the results come on google, if the results contain "Covid" it AUTOMATICALLY redirects you to YouTube, In my version I tired but it replace the word "covid" with the YouTube link, I don't get what I am Doing wrong.

<pre lang="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(/Covid/gi, "https://www.youtube.com");
  }
}


What I have tried:

I have tried using different functions, but none of the seem to work
Posted
Updated 23-Jan-22 16:02pm
v2
Comments
Richard Deeming 24-Jan-22 6:59am    
As I told your classmate / colleague / sock-puppet last week[^]: This kind of behaviour seems like a great way to get your extension banned as "malware".
ZMS 2022 24-Jan-22 8:15am    
Hello,
What last week are you talking about?
I am Not doing this for an actual extension, it's for this school project for my ICT class, IDK what you are talking about?
Can you Clarify? "the last week" part

1 solution

Try the following code to redirect as per specific word found:
<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;
	}
	
	function getSpecificWord(Element) {
	  var specificWord = "covid"; // Search your specific word here and return
	  return specificWord;
	}	
</script>
 
Share this answer
 
Comments
ZMS 2022 24-Jan-22 8:19am    
Bro, I Improved your code, but there is a problem, the Chrome page keeps reloading for no reason, Can you tell me what I have to do in the "Search your specific word" part?
Also here is the code I made, ur one may work, but IDK what you meant by Search your word here
LOOK:
(function redirectURL() {
var specWord = getSpecificWord();
switch (specWord) {
case 'corona':
window.location.replace('covid.html');
break;
case 'covid':
window.location.replace('covid.html');
break;
case 'covid-19':
window.location.replace('covid.html);
break;
default:
return true;
break;
}
return false;
})();

function getSpecificWord(Element) {
var specificWord = 'corona';
}


END
I know that it is not clear but if your code is right what do I do in the Part I mentioned, Sometimes the teacher is the problem, my teacher is literally the worst :(

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