top of page

Subscribe to get best practices, tutorials, and many other cool things directly to your email inbox

  • Writer's pictureAhmed Tarek

Learn How to Develop a JavaScript UserScript to Monitor Freelancer Projects and Post to Slack

Updated: Apr 17

Using Dependency Injection (DI) and Inversion of Control (IoC)


Learn develop a JavaScript UserScript to monitor Freelancer projects and post them to Slack using Dependency Injection (DI), Inversion of Control (IoC), API. Best Practice Code Coding Programming Software Development Architecture Engineering
Image by Ahmed Tarek

This article is about learning how to develop a UserScript that watches Freelancer.com projects notifications and sends them to your Slack channel. You can set the skills you are interested in and the script will send you notifications of the projects related to these skills only.


If you are not familiar with the term UserScripts, or don’t know how to set a UserScript to work, you can check my story How to Customize Webpages UI/Behavior Using JavaScript UserScripts.


 

Learn develop a JavaScript UserScript to monitor Freelancer projects and post them to Slack using Dependency Injection (DI), Inversion of Control (IoC), API. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Markus Spiske on Unsplash

These are some important notes which you need to keep in mind:

  1. Change EUR to your own preferred currency.

  2. Change [ “C# Programming”, “.NET”, “ASP.NET”, … ] to your array of skills. Please note that you should enter skills as they are defined by Freelancer.com.

  3. Change lk6f7fd0–277z-12ec-8894–41n9e18q3291 to your own freecurrencyapi.net API Key.

  4. Change xoxb-378377152752–2574471671501-pghLJ7MGiZ9KUKI8YzJQdRN2 to your own Slack Bot Key.

  5. Change G8XCG0FK5 to your own Slack channel Id.

  6. Change Freelancer Notifications Catcher to your preferred Slack bot username.

  7. You will need to leave Freelancer.com dashboard page open so that the UserScript could keep running.


 

Important Note:


Due to a problem with Slack API, you might get a CORS error on your browser console leading to a failure in posting projects to Slack.


To overcome this problem, you will need to turn off the CORS protection on your web browser. To do so for Chrome, you can create a shortcut to Chrome.exe and modify the target by adding the following parameters


— disable-web-security — user-data-dir=”C:\Users\{YourUsername}\AppData\Local\Google\Chrome\User Data\”

Learn develop a JavaScript UserScript to monitor Freelancer projects and post them to Slack using Dependency Injection (DI), Inversion of Control (IoC), API. Best Practice Code Coding Programming Software Development Architecture Engineering

 


 

Learn develop a JavaScript UserScript to monitor Freelancer projects and post them to Slack using Dependency Injection (DI), Inversion of Control (IoC), API. Best Practice Code Coding Programming Software Development Architecture Engineering

Time For Coding


In the next section, we will divide the whole code into smaller parts and explain each part. It is not only about writing code which would finally work, it is also about some best practices and design decisions.


This would help you understand the code and may be learn new skills. Therefore, buckle up and get ready for the ride.


 

Learn develop a JavaScript UserScript to monitor Freelancer projects and post them to Slack using Dependency Injection (DI), Inversion of Control (IoC), API. Best Practice Code Coding Programming Software Development Architecture Engineering

UserScript Implementation


Header



The important things to note here are:

  1. The UserScript is set to be active on http://www.freelancer.com/dashboard page only. This is intended in order not to spam your Slack channel with messages whenever any Freelancer page is opened.

  2. The UserScript needs jquery-3.3.1.min.js library to be loaded.



Freelancer Project



The important things to note here are:

  1. FreelancerProject is the class defining the object containing all info about a Freelancer project.

  2. It also implements isEqual function to be able to compare two instances and decide if they are exactly the same or not.

  3. This is a best practice as now all the logic and knowledge about a FreelancerProject internal properties is encapsulated and isolated inside the FreelancerProject itself.

  4. Therefore, whenever a change related to how to compare two projects comes in, we have only one place to change.



Currency Converter



The important things to note here are:

  1. CurrencyConverter is a service which converts between currencies.

  2. It is built upon the converter API provided by freecurrencyapi.net.

  3. You will need to go there, create a free account and get your API key to use it.

  4. The API takes a currency as an input and then it returns the conversion rate between this specific currency and all other currencies in an array.

  5. CurrencyConverter caches the results coming back from the API for each input currency. Whenever a currency -and its corresponding conversion rates- exists in the cache, CurrencyConverter would return the cached value, otherwise, a new API call would be performed and then the response would be saved to the cache.

  6. The cache would be invalidated by default every 12 hours unless it is overridden by passing another value for the parameter cachingSpanInMilliseconds while calling new CurrencyConverter(apiKey, cachingSpanInMilliseconds).

  7. The cache is saved in page memory which means that if you refresh the Freelancer dashboard page, the cache would be cleared.


 


 

Slacker



The important things to note here are:

  1. Slacker is a service which wraps Slack API.

  2. You will need to create your own free Slack account, create your own Channel, define an app and get your API tokens. You can find all the info and steps using Slack online documentation.

  3. Then, you should have a Slack Bot Key and a Channel Id.

  4. Slacker has the slackIt function which takes in a freelancerProject object as a parameter and then it performs the appropriate Slack API call to post the message to your Channel.

  5. Any details on how to use Slack APIs could be found on Slack API page.



Scrapper



The important things to note here are:

  1. Scrapper is the service responsible for scrapping/retrieving the info about a Freelancer project from the HTML notification card.

  2. Scrapper is dependent on the CurrencyConverter as it uses it to convert the project budget into your preferred currency if it is needed.

  3. However, the CurrencyConverter is injected into the Scrapper through its constructor rather than using a global instance.

  4. JavaScript Promise timeout technique is used here to avoid waiting forever for a Promise to get fulfilled. If you are interested into this topic, you can read more about it on my story How to Set JavaScript Promise Timeout.


 


 

Queue Manager



The important things to note here are:

  1. When a project info is scrapped and ready to be posted to your Slack Channel, this is not happening directly. First, the project info is queued into a queue and then another service is responsible for dequeuing from the queue and posting to your Slack Channel.

  2. This is a best practice as it decouples between the module scrapping projects information and the other module posting to Slack Channel.

  3. This minimizes the probability of having a single point of failure.

  4. QueueManager is the service responsible for dequeuing from the queue and posting to your Slack Channel.

  5. QueueManager is dependent on the Slacker as it uses it to post the project info to your Slack Channel.

  6. However, the Slacker is injected into the QueueManager through its constructor rather than using a global instance.



Freelancer Project Evaluator



The important things to note here are:

  1. FreelancerProjectEvaluator is the service responsible for evaluating if you are interested into a certain Freelancer project or not.

  2. The evaluation is done based on the array of skills filled by you and passed in to the FreelancerProjectEvaluator constructor.

  3. The skills in the array should be filled exactly as they are defined by Freelancer.com.


 


 

Freelancer Notifications Watcher



The important things to note here are:

  1. FreelancerNotificationsWatcher is the service responsible for monitoring the Freelancer dashboard page for any project notifications.

  2. FreelancerNotificationsWatcher is dependent on the Scrapper, FreelancerProjectEvaluator and QueueManager.

  3. The Scrapper is used to scrap the project info from the HTML notification.

  4. The FreelancerProjectEvaluator is used to evaluate if you should be notified about the new project or not.

  5. The QueueManager is used to queue the new project info if needed.

  6. The dependencies are injected into the FreelancerNotificationsWatcher through its constructor rather than using global instances.



Utilities



The important things to note here are:

  1. replaceRecurively is an extension for the native String class to enable replacing a string with another in recursive manner.

  2. trimStart is an extension for the native String class to enable removing a string from the front end in recursive manner.

  3. removeCurrencySymbolsPrefix is an extension for the native String class to enable removing currency symbols from the front end.

  4. getWordsFromString is a function to split a string into words.

  5. wait is a function to wait for a certain amount of time and then resolves a Promise.



Initialization



The important things to note here are:

  1. const preferredCurrency = “EUR”; sets the preferred currency to “EUR”. Change this as per your own preference.

  2. const freelancerSkillsToWatch = […] sets the Freelancer skills you are interested into. Change this as per your own preferences.

  3. const currencyConverter = new CurrencyConverter(“lk6f7fd0–277z-12ec-8894–41n9e18q3291”, 43200000); initializes an instance of the CurrencyConverter and sets the caching interval for 12 hours (432,000,00 milliseconds). You will need to change the API key to the one you got from freecurrencyapi.net.

  4. const slacker = new Slacker(“xoxb-378377152752–2574471671501-pghLJ7MGiZ9KUKI8YzJQdRN2”, “G8XCG0FK5”, “Freelancer Notifications Catcher”, preferredCurrency); initializes an instance of the slacker. You will need to change the Slack Bot Key and the Channel Id to the ones you got from Slack.

  5. const queueManager = new QueueManager(slacker, 5000); initializes an instance of the QueueManager and sets the watch interval to 5 seconds (5000 milliseconds).

  6. const scrapper = new Scrapper(currencyConverter, preferredCurrency); initializes an instance of the Scrapper.

  7. const freelancerProjectEvaluator = new FreelancerProjectEvaluator(freelancerSkillsToWatch); initializes an instance of the FreelancerProjectEvaluator.

  8. const freelancerNotificationsWatcher = new FreelancerNotificationsWatcher(scrapper, freelancerProjectEvaluator, queueManager, 2000); initializes an instance of the FreelancerNotificationsWatcher and sets the watch interval to 2 seconds (2000 milliseconds).



Running the UserScript



This is where we trigger the FreelancerNotificationsWatcher and QueueManager to start their work.


Finally, hope you found reading this article as interesting as I found writing it.




Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Subscribe to get best practices, tutorials, and many other cool things directly to your email inbox

bottom of page