ProtectLib.js Library Documentation

ProtectLib.js is a useful JavaScript library that serves as an efficient way of preventing and discouraging users from hacking the client side of web applications! You can manually direct it to protect the following aspects of your code from users and untrusted third-party scripts:

You can test ProtectLib.js v1.0 at the ProtectLib.js Playground.


Warnings & Notices


Installation

Before installing Protectlib.js in the <head> tag of your HTML file, add this line of code:
<script>window.authorized = [];</script>
Inside the empty array, add files that you want to have access to all Protectlib.js functions. The main HTML page is added automatically, so you do not have to add it. If you want to use a file in the same domain, you can access it by its relative filename. If you want to use a file in another domain, use its URL.
Here is an example of an array that gives access for Protectlib.js functions to a file called example.js and a file whose URL is https://example.com/example.js:
<script>window.authorized = ["example.js", "https://example.com/example.js"];</script>

Once you have specified the permitted files, you can install ProtectLib.js in one of the following ways:


getFileName() Function

The getFileName() function is used to get the file that a series of scripts started in.

Parameters

Returns

Example Usage (from a file called example.html in the same directory as this documentation)

// returns "https://zacharym6.codewizardshq.com/libraries/protectlib/v1.0/example.html"
getFileName();

Example Usage (from a file called example.html in the same directory as this documentation)

<!-- returns "https://zacharym6.codewizardshq.com/libraries/protectlib/v1.0/example.html"
     when the button is clicked -->
<button onclick="getFileName();">Click me!</button>

Example Usage (from a file called example.js in the same directory as this documentation)

// returns "https://zacharym6.codewizardshq.com/libraries/protectlib/v1.0/example.js"
getFileName();

Example Usage (when called by the browser's Developer Tools)

// returns "<anonymous>"
getFileName();

Notes


protectDOM() Function

The protectDOM() function is used to prevent changes to the DOM (Document Object Model).

Parameters

Returns

Example Usage

// Note the use of `const` in this instance. This prevents the object from being overridden by untrusted sources.
const domToggler = protectDOM(["example.js", "https://example.com/example.js"]);

Notes


protectFunction() Function

The protectFunction() function is used to prevent functions from being used by untrusted scripts.

Parameters

Returns

Example Usage

function testFunctionSource() {
  console.log("This is a test!");
}

// Note the use of `const` in this instance. This prevents the function from being overridden by untrusted sources.
const testFunction = protectFunction("testFunctionSource", ["example.js", "https://example.com/example.js"]);
testFunctionSource();  // logs "This function has been protected by ProtectLib.js!"
testFunction();  // logs "This is a test!"

Notes


protectVariable() Function

The protectVariable() function is used to prevent changes or access to a variable from untrusted sources.

Parameters

Returns

Example Usage

// Note the use of `const` in this instance. This prevents the variable from being overridden by untrusted sources.
const myVariable = protectVariable("a string value", ["example.html", "example.js"], false);

Notes


Accessing the DOM

If you used ProtectLib.js to defend your DOM, you probably will want to know how you can make changes to the DOM. In order to do so, you must use the object which was returned by the protectDOM() function.

To edit the DOM, you must follow these steps:

  1. Make sure you're accessing the DOM from a permitted file. This is the accessFiles array which was sent to the protectDOM() function.
  2. Stop the DOM observer. This can be done by using the pause() function within the object which was returned by the protectDOM() function.
  3. Make the intended changes to the elements.
  4. Restart the DOM observer. This can be done by using the play() function within the object which was returned by the protectDOM() function.

Here's an example script showing how you can edit the DOM using the above steps:

// define the DOM observer (assumes the file running this was granted permission in the window.authorized array)
const domToggler = protectDOM();
// stop the DOM observer
domToggler.pause();
// make the intended changes
document.querySelector("#edit-this-element").innerHTML += "<p>Changes were made to an element!</p>";
// restart the DOM observer
domToggler.play();

Accessing Variables

Getting a Variable

If you used ProtectLib.js to protect a variable using the protectVariable() function, you can retrieve its value while in an allowed file with the .get() function. Here's an example getting the value (false) from a variable called gameOver:

// Defining the variable
const gameOver = protectVariable(false, ["example.html"], true);
// Getting the variable's value (logs `false` to the console)
console.log(gameOver.get());
Setting a Variable

If you used ProtectLib.js to protect a variable using the protectVariable() function, and you did not set the third parameter of the function to true, then you can change the value of your variable in a permitted file. For example:

// Defining the variable
const userId = protectVariable(0, ["example.html"], false);
// Setting the variable's value to 1
userId.set(1);
// Getting the variable's new value (logs `1` to the console)
console.log(userId.get());

Contact Z! About This Library

Subject: ProtectLib.js Version 1.0


The Developer will receive your message in his own good time. No messages will receive replies by any known or unknown method of communication. Any suggestions you propose will be considered. Your message will be stored in a public database file.