Skip to content

Commit 794e937

Browse files
Moved files from old repositories
0 parents  commit 794e937

9 files changed

Lines changed: 289 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/**
2+
*.iml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 juztcode
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# JS Hacks
2+
JS hacks to simplify tasks.
3+
4+
>Happy Coding :)

console-suspend/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Suspend console logs
2+
Small library that can be used to suspend console logs.
3+
4+
## Why use this?
5+
* Console is filled with unwanted log lines and cant find where its originated.
6+
* Need to suspend unwanted log lines.
7+
8+
## How to add?
9+
* Add link to the html file
10+
```html
11+
<script src="https://cdn.jsdelivr.net/gh/juztcode/js-hacks/console-suspend/console-suspend.js"></script>
12+
```
13+
14+
* Initialize with a list of log lines
15+
```html
16+
<script>
17+
ConsoleSuspend([
18+
'*****************************',
19+
'* This is a test log line *'
20+
]);
21+
</script>
22+
```
23+
24+
* That's it you can suspend unwanted log lines.
25+
26+
* ###### This can also suspend log lines if them match the given pattern.
27+
28+
>Happy Coding :)

console-suspend/console-suspend.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const ConsoleSuspend = function (list) {
2+
const log = console.log;
3+
const info = console.info;
4+
const warn = console.warn;
5+
const debug = console.debug;
6+
const error = console.error;
7+
8+
const isValid = function (args) {
9+
const s = args[0];
10+
11+
for (const i of list) {
12+
const type = typeof s;
13+
if (type === 'string' && s.startsWith(i)) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
};
20+
21+
console.log = function () {
22+
const args = Array.prototype.slice.call(arguments);
23+
24+
if (isValid(args)) {
25+
log.apply(console, args);
26+
}
27+
};
28+
29+
console.info = function () {
30+
const args = Array.prototype.slice.call(arguments);
31+
32+
if (isValid(args)) {
33+
info.apply(console, args);
34+
}
35+
};
36+
37+
console.debug = function () {
38+
const args = Array.prototype.slice.call(arguments);
39+
40+
if (isValid(args)) {
41+
debug.apply(console, args);
42+
}
43+
};
44+
45+
console.warn = function () {
46+
const args = Array.prototype.slice.call(arguments);
47+
48+
if (isValid(args)) {
49+
warn.apply(console, args);
50+
}
51+
};
52+
53+
console.error = function () {
54+
const args = Array.prototype.slice.call(arguments);
55+
56+
if (isValid(args)) {
57+
error.apply(console, args);
58+
}
59+
};
60+
};

console-to-html/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Bring console to html
2+
Small library that can be used to display console messages inside html view.
3+
4+
## Why use this?
5+
* Tired of opening console to view log messages.
6+
* Need to display logs to user with minimum implementation
7+
8+
## How to add?
9+
* Add link to the html file
10+
```html
11+
<script src="https://cdn.jsdelivr.net/gh/juztcode/js-hacks/console-to-html/console-to-html.js"></script>
12+
```
13+
14+
* Initialize with html element id that you are using to display logs
15+
```html
16+
<script>
17+
ConsoleToHTML('elementId');
18+
</script>
19+
```
20+
21+
* Thats it you can use console functions and it will display inside html
22+
23+
## What are the functions?
24+
* console.log()
25+
* console.info()
26+
* console.debug()
27+
* console.warn()
28+
* console.error()
29+
* console.clear()
30+
31+
>Happy Coding :)

console-to-html/console-to-html.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var ConsoleToHTML = function (elementId) {
2+
var log = console.log;
3+
var info = console.info;
4+
var warn = console.warn;
5+
var debug = console.debug;
6+
var error = console.error;
7+
var clear = console.clear;
8+
9+
console.log = function () {
10+
var args = Array.prototype.slice.call(arguments);
11+
var concatenatedString = concatStrings(args);
12+
log.apply(console, args);
13+
appendToElement('<p>' + '[log] ' + concatenatedString + '</p>');
14+
};
15+
16+
console.info = function () {
17+
var args = Array.prototype.slice.call(arguments);
18+
var concatenatedString = concatStrings(args);
19+
info.apply(console, args);
20+
appendToElement('<p>' + '[info] ' + concatenatedString + '</p>');
21+
};
22+
23+
console.debug = function () {
24+
var args = Array.prototype.slice.call(arguments);
25+
var concatenatedString = concatStrings(args);
26+
debug.apply(console, args);
27+
appendToElement('<p>' + '[debug] ' + concatenatedString + '</p>');
28+
};
29+
30+
console.warn = function () {
31+
var args = Array.prototype.slice.call(arguments);
32+
var concatenatedString = concatStrings(args);
33+
warn.apply(console, args);
34+
appendToElement('<p style="color: yellow">' + '[warn] ' + +concatenatedString + '</p>');
35+
};
36+
37+
console.error = function () {
38+
var args = Array.prototype.slice.call(arguments);
39+
var concatenatedString = concatStrings(args);
40+
error.apply(console, args);
41+
appendToElement('<p style="color: red">' + '[error] ' + concatenatedString + '</p>');
42+
};
43+
44+
console.clear = function () {
45+
clear();
46+
clearElement();
47+
};
48+
49+
var concatStrings = function (args) {
50+
var result = '';
51+
52+
for (var i in args) {
53+
var arg = args[i];
54+
if (typeof arg === 'object') {
55+
result += ' ' + JSON.stringify(arg);
56+
} else {
57+
result += ' ' + arg;
58+
}
59+
}
60+
61+
return result;
62+
};
63+
64+
var appendToElement = function (htmlString) {
65+
var element = document.getElementById(elementId);
66+
element.insertAdjacentHTML('beforeend', htmlString);
67+
element.scrollTop = element.lastChild.offsetTop;
68+
};
69+
70+
var clearElement = function () {
71+
var element = document.getElementById(elementId);
72+
element.innerHTML = '';
73+
};
74+
};

http-filter/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Http Filter
2+
Small library that can be used to filter XMLHttpRequest based on the hostname.
3+
4+
## Why use this?
5+
* Need a way to find all http calls from third party javascript libraries
6+
* Need to prevent unwanted http calls from javascript code.
7+
8+
## How to add?
9+
* Add link to the html file
10+
```html
11+
<script src="https://cdn.jsdelivr.net/gh/juztcode/js-hacks/http-filter/http-filter@0.0.1/src/http-filter.js"></script>
12+
```
13+
14+
* Initialize with accepted hostname list and match type ('exact', 'partial')
15+
```html
16+
<script>
17+
HttpFilter([
18+
'sample.com'
19+
], 'exact');
20+
</script>
21+
```
22+
23+
* That's it your http call will filter according to their hostname
24+
25+
>Happy Coding :)

http-filter/http-filter.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const HttpFilter = function (acceptedDomains, matchType) {
2+
window.matchType = matchType;
3+
window.acceptedDomains = acceptedDomains;
4+
};
5+
6+
const extractHostname = function (url) {
7+
let hostname;
8+
9+
// find & remove protocol (http, ftp, etc.) and get hostname
10+
if (url.indexOf("//") > -1) {
11+
hostname = url.split('/')[2];
12+
} else {
13+
hostname = url.split('/')[0];
14+
}
15+
16+
// find & remove port number
17+
hostname = hostname.split(':')[0];
18+
// find & remove "?"
19+
hostname = hostname.split('?')[0];
20+
21+
return hostname;
22+
};
23+
24+
const canSendRequest = function (hostname) {
25+
if (window.matchType === 'exact') {
26+
return window.acceptedDomains.indexOf(hostname) > -1;
27+
} else {
28+
return !!window.acceptedDomains.find(d => d.includes(hostname))
29+
}
30+
};
31+
32+
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
33+
const hostname = extractHostname(url);
34+
35+
if (canSendRequest(hostname)) {
36+
// send request
37+
open.call(this, method, url, async, user, pass);
38+
} else {
39+
// log error end throw exception
40+
const message = `hostname: "${hostname}" blocked from http filter`;
41+
console.error(message);
42+
throw new Error(message);
43+
}
44+
};

0 commit comments

Comments
 (0)