четверг, 25 марта 2021 г.

Package JavaScript code with Node.js in one executable file using PKG

Package JavaScript code with Node.js in one executable file using PKG.

1) Install library PKG:

npm install pkg

2) Open in your code editor file node_modules/pkg/lib-es5/fabricator.js

3) Find in this file row:

child = children[key] = (0, _child_process.spawn)(cmd, bakes.concat('-e', script), {

4) After this row add line of code to fix administarrtor rights problem:

shell: true,

5) Save your changes in file node_modules/pkg/lib-es5/fabricator.js

6) Open command line ("CMD") and type in it:

cd "... here is path to folder with your source code..."

Then press button "Enter" on your keyboard.

7) Then type in command line:

set NODE_TLS_REJECT_UNAUTHORIZED=0

and press "Enter" again.

This will fix SSL problem.

(If your want to run PKG library from your custom source code you can also write in it:

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

to do the same.)

8) Then type in command line for example:

node "D:\Work\Your application\node_modules\pkg\lib-es5\bin.js" your-start-file.js --debug --targets node14-windows-x64

to package your JavaScript application files together with Node.js in one executable file.


Execute JavaScript code in browser address bar

 How to execute JavaScript code in browser address bar?

1) Clear your browser address bar.

2) Copy this line of JavaScript code:

function showlog (string) {var pre = document.createElement('pre'); pre.innerHTML = string; document.getElementById('sbpllogs').appendChild(pre);} var d = document.createElement('div'); d.style.position = 'absolute'; d.style.zIndex = 100000; d.style.top = '0px'; d.style.left = '0px'; d.innerHTML = '<div style="position: relative;"><button id="sbplhideshow" onclick="(function () {' + "var self = document.getElementById('sbplhideshow'); var helpers = Array.prototype.slice.call(document.getElementsByClassName('sbplhelper')); if (self.innerHTML === 'HIDE') {self.innerHTML = 'SHOW'; helpers.forEach(function (helper) {helper.style.display = 'none';});} else {self.innerHTML = 'HIDE'; helpers.forEach(function (helper) {helper.style.display = 'inline-block';});}" + '})()" style="width: 100px; height: 25px; border: 1px solid black; cursor: pointer;">HIDE</button><button class="sbplhelper" onclick="(function () {' + "document.getElementById('sbpleditor').value = '';" + '})()" style="width: 100px; height: 25px; border: 1px solid black; cursor: pointer;">CLEAR</button><button class="sbplhelper" onclick="(function () {' + "var sbpleditor = document.getElementById('sbpleditor'); try {eval(sbpleditor.value); sbpleditor.value = '';} catch (e) {showlog(e); sbpleditor.value = 'ERROR'}" + '})()" style="width: 100px; height: 25px; border: 1px solid black; cursor: pointer;">RUN</button><br /><textarea id="sbpleditor" class="sbplhelper" style="display: inline-block; width: 500px; height: 200px; padding: 4px; background-color: white; border: 1px solid black; font-size: 16px;"></textarea><div id="sbpllogs" class="sbplhelper" style="overflow: auto; display: inline-block; width: 500px; height: 200px; padding: 0px 4px 0px 4px; background-color: white; border: 1px solid black; font-size: 16px;"></div></div>'; document.body.appendChild(d);

3) Paste this line into address bar.

4) Press together on keyboard buttons "Ctrl" and "A" to select all pasted line in the borwser address bar.

5) After that press "Left arrow" on keyboard. In result cursor in the address bar will move to the start of the line.

6) Now type on keyboard (with colon and without space);

javascript:

Result in your address bar will look like that:

[javasript:function showlog (string) {var pre = docu...]

7) Then just press "Enter" on your keyboard. This will execute JavaScript code in address bar.

8) This code will show on your page custom debug window with buttons: "HIDE", "CLEAR", "RUN" and two panels. 

In left panel you can enter your JavaScript code. 

In right panel you can see logs that will show there if you execute in the left panel custom function "showlog()".

For example.

Type in left panel this code:

var a = 10;

showlog("a equals " + a);

Then click on button "RUN".

In right panel you will see:

a equals 10

If you click on button "CLEAR" you will erase all code in the left panel.

If you click on button "HIDE" you will minimize custom debug window. 

After that you can click on button "SHOW" to show custom debug window in full size.