0

I have a txt file with some words in each line. I wish to have a small js file that does nothing else but check the current URL and if the URL contains any of the words from the txt file, then pops an alert. How do I do that? (I'm not familiar with JS at all, sorry)

Bert
  • 347
  • 7
  • 25
  • 1
    The main problem I see is that webpages are not allowed to read files on your computer. So as a prerequisite, you must create a webpage with a `` element and the user (you?) will have to select a file there. – autra Sep 28 '18 at 12:31
  • How about then if I just create an array of the words from the txt file and then check element by element if the URL contains any of them? – Bert Sep 28 '18 at 12:37

2 Answers2

1

I assume you are in a browser environment (opposed to nodejs).

If you create an array of the words in a variable, then you can combine Array.prototype.some and String.includes to achieve your need.

You'll call .some on your word array, giving as argument a function which will check if the current word is in the url by using String.includes.

The current url can be obtained with the location object.

If you're not familiar with js at all, do spend some time on mdn, it's one of the best javascript documentation available online. They also have a nice collection of javascript guides

autra
  • 895
  • 6
  • 21
  • Thanks Mate. I was looking for a copy-paste sollution, but I'll try to make the most of it. :) This will be a Chrome Extension BTW – Bert Sep 28 '18 at 12:53
  • "I was looking for a copy-paste solution" I knew it :-) What is the good in that? It's better to learn things in the process and to gain some autonomy... Apart from that, you should have mentioned that this would be a chrome extension in your question, because it changes everything: you can activate your chrome extension only for some url, see https://developer.chrome.com/extensions/getstarted. Also, I'm pretty sure extensions *can* read local files. – autra Sep 28 '18 at 13:07
  • 1; I'm not a developer, only a system operator, builder and designer learning about sys security right now. :) 2; Sorry for not mentioning 3; I'm also sure it will be able to read local files, but I'm getting tired of trying to find the solution. So I'll just create an array of elements with the words and check that against the current URL. I think this will be the cleanest way – Bert Sep 28 '18 at 13:16
  • So I've came up with the following: `if (badurls.indexOf(currenturl) > -1) {...}` OK, this is working just fine. The only issue is that only exact matches are coming back with any value. How do I need to change it to come back with a correct value if one of the array elements only CONTAIN the string I'm looking for? For example: Searching for: `berry`. So `raspberry` should be a valid find for it. – Bert Sep 28 '18 at 15:44
  • Checking against an array is completely ok :-) – autra Sep 28 '18 at 16:05
  • How?? Tell me how! :D Pretty please!! – Bert Sep 28 '18 at 16:06
  • Just read again my response: You call `.some` on your words array. `some` takes as argument a function that will be called with every word and must return a boolean. If one of the call returns true, `some` will return true (have you read the documentation I linked, please do! there's no programming without reading documentation)? And this boolean is just `currentUrl.includes(word)`. Can you now wrap everything together? – autra Sep 28 '18 at 16:11
  • 1
    `var arrayLength = badurls.length; function urlCheck() { for (var i = 0; i < arrayLength; i++) { if ( currenturl.includes(badurls[i]) ) { alert("kamu"); break; } else { } } } urlCheck()` Arigato! :) – Bert Sep 28 '18 at 16:13
  • This also works! I was thinking about `var isBadUrl = badurls.some(word => currenturl.includes(word))`, but what you wrote is essentially the same :-) – autra Sep 28 '18 at 16:41
-1

you read the file line by line and use regular expression to check if the word is in the url or not. You can refer below links on how to do it:

Read a file one line at a time in node.js?

whole word match in javascript

Nilesh Jain
  • 106
  • 8
  • 1
    "Check the current URL" I think @Bert is not using nodejs – autra Sep 28 '18 at 12:38
  • What you are saying is this is a duplicate of your links and the question should be voted to be closed for those reasons. – Rob Sep 28 '18 at 12:41