Unique in generated random number (no duplicate) - javascript

I want only the generated random number will hava no duplicate, whenever i try to put the number, the generated random number has duplicate, any other idea. what should i change here?
See the fiddle
var arr = hello.toString(10).replace(/\D/g, '0').split('').map(Number);
for(i=0;i<arr.length;i++) arr[i] = +arr[i]|0;
//initialize variables
var z = arr[3];
var y = arr[2];
var x = arr[1];
var w = arr[0];
while((((a2 <= 0) || (a2 > 49)) || ((b <= 0) || (b > 49)) || ((c <= 0) || (c > 49)) || ((d <= 0) || (d > 49)) || ((e2 <= 0) || (e2 > 49)) || ((f <= 0) || (f > 49)) || ((g <= 0) || (g > 49) ))){
//loop ulit kapag hindi match yung random number sa value nung z
while( zRandomString != z){
zRandom = Math.floor(Math.random() * (109 - 100 + 1)) + 100;
zRandomRound = zRandom % 10;
zRandomString = zRandomRound.toString();
}
var zNew = zRandom; //new value ng z
document.getElementById("zebra").innerHTML = "Z = " + zNew + "<br />";// udsadsadsad
h = zNew;
while( yRandomString != y){
yRandom = Math.floor(Math.random() * (49 - 1 + 1)) + 1;
yRandomRound = yRandom % 10;
yRandomString = yRandomRound.toString();
}
var yNew = yRandom; //new value ng z
document.getElementById("yeah").innerHTML = "Y = " + yNew + "<br />";// udsadsadsad
h = h - yNew;
while( xRandomString != x){
xRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
xRandomRound = xRandom % 10;
xRandomString = xRandomRound.toString();
}
var xNew = xRandom; //new value ng z
document.getElementById("ex").innerHTML = "X = " + xNew + "<br />";// udsadsadsad
h = h - xNew;
while( wRandomString != w){
wRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
wRandomRound = wRandom % 10;
wRandomString = wRandomRound.toString();
}
var wNew = wRandom; //new value ng z
document.getElementById("weh").innerHTML = "W = " + wNew + "<br />";// udsadsadsad
//h = Math.abs(h - wNew); // new value of h
h = h - wNew;
a = Math.floor(Math.random() * (wNew - 1 + 1)) + 1;
a2 = wNew - a;
b = Math.floor(Math.random() * (a2 - 1 + 1)) + 1;
c = a - b;
d = yNew;
e = Math.floor(Math.random() * (xNew - 1 + 1)) + 1;
e2 = xNew - e;
f = Math.floor(Math.random() * (e2 - 1 + 1)) + 1;
g = e - f;
}
var combo = a2.toString() + ', ' + b.toString() + ', ' + c.toString() + ', ' + d.toString() + ', ' + e2.toString() + ', ' + f.toString() + ', ' + g.toString() + ', ' + h.toString();
document.getElementById("combo").innerHTML = combo;

Try to use this scheme:
var r;
do {
r = Math.floor(Math.random() * 10);
} while (r == 6);

It is not quite clear what you want, but one way to create a sequence on unique random numbers is to create a pool of numbers and sucessively pick and remove items from it:
function pick(pool) {
if (pool.length == 0) return undefined;
// pick an element from the pool
var k = (Math.random() * pool.length) | 0;
var res = pool[k];
// adjust array
pool[k] = pool[pool.length - 1];
pool.pop();
return res;
}
// example
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
while (pool.length) {
console.log(pick(pool));
}
Another method is to shoffle the pool first and then pop off elements:
function shuffle(arr) {
var n = arr.length;
while (n) {
// pick element
var k = (Math.random() * n--) | 0;
// swap last and picked elements
var swap = arr[k];
arr[k] = arr[n];
arr[n] = swap;
}
}
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle(pool);
while (pool.length) {
console.log(pool.pop());
}
These two methods aren't very different if you look at the pick and shuffle functions. Of course, eventually you will exhaust the pool. You can then decide to recreate or reshuffle the array. Also note that these methods will produce repeated elements if the pool has repeated entries.

Related

using Damerau-Levenshtein distance to compare sets of text in code.org

Not very knowledgeable with coding, I usually use block coding and not typing.
I've used many different Levenshtein distance codes I've found online and most of them didn't work for one reason or another
var levDist = function (s, t) {
var d = []; //2d matrix
// Step 1
var n = s.length;
var m = t.length;
if (n == 0) return m;
if (m == 0) return n;
//Create an array of arrays in javascript (a descending loop is quicker)
for (var i = n; i >= 0; i--) d[i] = [];
// Step 2
for (i = n; i >= 0; i--) d[i][0] = i;
for (var j = m; j >= 0; j--) d[0][j] = j;
// Step 3
for (i = 1; i <= n; i++) {
var s_i = s.charAt(i - 1);
// Step 4
for (j = 1; j <= m; j++) {
//Check the jagged ld total so far
if (i == j && d[i][j] > 4) return n;
var t_j = t.charAt(j - 1);
var cost = (s_i == t_j) ? 0 : 1; // Step 5
//Calculate the minimum
var mi = d[i - 1][j] + 1;
var b = d[i][j - 1] + 1;
var c = d[i - 1][j - 1] + cost;
if (b < mi) mi = b;
if (c < mi) mi = c;
d[i][j] = mi; // Step 6
//Damerau transposition
if (i > 1 && j > 1 && s_i == t.charAt(j - 2) && s.charAt(i - 2) == t_j) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}
// Step 7
return d[n][m];
};
This is all the code I’ve written (including the most recent attempt of getting the levenshtein distance)
var levDist = function (s, t) {
var d = []; //2d matrix
// Step 1
var n = s.length;
var m = t.length;
if (n == 0) return m;
if (m == 0) return n;
//Create an array of arrays in javascript (a descending loop is quicker)
for (var i = n; i >= 0; i--) d[i] = [];
// Step 2
for (i = n; i >= 0; i--) d[i][0] = i;
for (var j = m; j >= 0; j--) d[0][j] = j;
// Step 3
for (i = 1; i <= n; i++) {
var s_i = s.charAt(i - 1);
// Step 4
for (j = 1; j <= m; j++) {
//Check the jagged ld total so far
if (i == j && d[i][j] > 4) return n;
var t_j = t.charAt(j - 1);
var cost = (s_i == t_j) ? 0 : 1; // Step 5
//Calculate the minimum
var mi = d[i - 1][j] + 1;
var b = d[i][j - 1] + 1;
var c = d[i - 1][j - 1] + cost;
if (b < mi) mi = b;
if (c < mi) mi = c;
d[i][j] = mi; // Step 6
//Damerau transposition
if (i > 1 && j > 1 && s_i == t.charAt(j - 2) && s.charAt(i - 2) == t_j) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}
// Step 7
return d[n][m];
};
var S = "Hello World";
var grossWPM;
var Transparency = 1;
var Timer = 60;
var InitialTime = Timer;
var Texts = getColumn("Texts", "Texts");
var TextLength = getColumn("Texts", "Number of Characters");
var Title = getColumn("Texts", "Titles");
var Author = getColumn("Texts", "Authors");
var TextSelector = randomNumber(0, 19);
console.log("Article #" + (TextSelector + 1));
console.log(TextLength[TextSelector] + " Characters in total");
console.log(Title[TextSelector]);
console.log("By: " + Author[TextSelector]);
var Countdown;
var Countdown = 6;
//Texts are obtained from
//https://data.typeracer.com/pit/texts
onEvent("button1", "click", function( ) {
timedLoop(1000, function() {
Countdown = Countdown - 1;
setText("button1", Countdown - 0);
timedLoop(100, function() {
setText("text_area2", "");
});
if (Countdown <= 1) {
stopTimedLoop();
setTimeout(function() {
setText("button1", "GO!");
setText("text_area1", Texts[TextSelector]);
if (getText("button1") == "GO!") {
var TransparentLoop = timedLoop(100, function() {
Transparency = Transparency - 0.1;
setProperty("Warning", "text-color", rgb(77,87,95, Transparency));
if (Transparency <= 0) {
deleteElement("Warning");
showElement("label2");
stopTimedLoop(TransparentLoop);
}
});
var TimerLoop = timedLoop(1000, function() {
Timer = Timer - 1;
setText("label2", Timer);
if (Timer <= 0) {
grossWPM = (TextLength[TextSelector] / 5) / ((InitialTime - Timer) / 60);
console.log(grossWPM);
setScreen("screen2");
if (Timer == 1) {
S = " second";
} else {
S = " seconds";
}
setText("label1", "Your typing speed was approximately " + (Math.round(grossWPM) + (" WPM* with " + (Timer + (S + " left")))));
stopTimedLoop(TimerLoop);
}
});
console.log("Timer Started");
timedLoop(10, function() {
var str = getText("text_area2");
if (str.length == TextLength[TextSelector]) {
stopTimedLoop(TimerLoop);
grossWPM = (TextLength[TextSelector] / 5) / ((InitialTime - Timer) / 60);
setScreen("screen2");
levDist(str, Texts[TextSelector]);
if (Timer == 1) {
S = " second";
} else {
S = " seconds";
}
setText("label1", "Your typing speed was approximately " + (Math.round(grossWPM) + (" WPM* with " + (Timer + (S + " left")))));
if (grossWPM == 69) {
setText("label4", "Nice");
}
stopTimedLoop();
}
});
}
}, 1000);
}
});
});
Obviously not that good at this so can anyone help?
I want to compare two sets of text
Something the user types in.
Paragraph that the user was supposed to type.
This is for a WPM test and I want a way to get a measurement for WPM that includes errors the user makes while typing.
If there is a way to check this besides the Levenshtein distance please tell me, I just looked up a way to do that and Levenshtein distance seemed like the way to do so
The error given by code.org says:
ERROR: Line: 50: TypeError: d[n] is undefined
I fixed the issue, I used this code
function levenshtein(s1, s2) {
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len;
}
if (s2_len === 0) {
return s1_len;
}
// BEGIN STATIC
var split = false;
try {
split = !('0')[0];
} catch (e) {
// Earlier IE may not support access by string index
split = true;
}
// END STATIC
if (split) {
s1 = s1.split('');
s2 = s2.split('');
}
var v0 = new Array(s1_len + 1);
var v1 = new Array(s1_len + 1);
var s1_idx = 0,
s2_idx = 0,
cost = 0;
for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) {
v0[s1_idx] = s1_idx;
}
var char_s1 = '',
char_s2 = '';
for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) {
v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
char_s1 = s1[s1_idx];
cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx + 1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) {
m_min = b;
}
if (c < m_min) {
m_min = c;
}
v1[s1_idx + 1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp;
}
return v0[s1_len];
}
and I got that code from this question
This is levenshtein distance NOT damerau-levenshtein distance

How to update innerHTML in very long running loop javascript

var star = 4125;
var moon = 2946;
var max = 50;
//-----------------------------------------------------------------------------------
var a,b,c,d,e,f;
var starx,moonx,tokenx;
var token = 0;
var txt;
for (a = max ; a >= 0 ; a--)
{
for (b = 0 ; b <= max ; b++)
{
for (c = 0 ; c <= max ; c++)
{
for (d = 0 ; d <= max ; d++)
{
for (e = 0 ; e <= max ; e++)
{
for (f = 0 ; f <= max ; f++)
{
starx = 75 * (a + b + c + d + e + f);
moonx = (10 * a) + (30 * b) + (50 * c) + (75 * d) + (125 * e) + (200 * f);
tokenx = (210 * a) + (235 * b) + (260 * c) + (300 * d) + (375 * e) + (500 * f);
if (starx <= star && moonx <= moon && tokenx >= token)
{
token = tokenx;
txt = tokenx + ',' + starx + ',' + moonx + ',' + a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f + '<br>\n';
document.getElementById("txt").innerHTML += txt;
console.log(txt);
}
}
}
}
}
}
}
Need help. I am very new to javascript. I would like to run those javascript in android. innerHTML don't update until the loop completed. And can't see console log in android too.
If you want to show the output of the loop at each iteration you can use setInterval()
like so:
for(let a = 0; a < 10; a++) {
document.body.innerHTML += "Hello world!" +a +"<br />";
}
document.body.innerHTML += "<br /> Loop but shows output at each iteration: <br />";
let a = 0;
let aLoop = setInterval(function() {
if(a < 10) {
document.body.innerHTML += "Hello world!" +a +"<br />";
a++;
} else {
clearInterval(iLoop);
}
}, 100);
However, this would require you to convert a lot of your code and would get messy. If all you wish to do is see the output on android you can try alert(txt); to see the output instead.
Use generator function* to yield text. DOM element innerHTML could be updated via setInterval timer.
function* cal() {
var star = 4125;
var moon = 2946;
var max = 50;
var a, b, c, d, e, f;
var starx, moonx, tokenx;
var token = 0;
var txt;
for (a = max; a >= 0; a--) {
for (b = 0; b <= max; b++) {
for (c = 0; c <= max; c++) {
for (d = 0; d <= max; d++) {
for (e = 0; e <= max; e++) {
for (f = 0; f <= max; f++) {
starx = 75 * (a + b + c + d + e + f);
moonx = (10 * a) + (30 * b) + (50 * c) + (75 * d) + (125 * e) + (200 * f);
tokenx = (210 * a) + (235 * b) + (260 * c) + (300 * d) + (375 * e) + (500 * f);
if (starx <= star && moonx <= moon && tokenx >= token) {
token = tokenx;
txt = tokenx + ',' + starx + ',' + moonx + ',' + a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f + '<br>\n';
// document.getElementById("txt").innerHTML += txt;
console.log(txt);
yield txt;
}
}
}
}
}
}
}
}
function run() {
gen = cal();
setInterval(()=> document.getElementById("txt").innerHTML +=
gen.next().value, 10);
}
run();
Web workers can do long running task.
See here:
https://www.w3schools.com/html/html5_webworkers.asp
Notes:
Chrome doesn't support web workers when load script from local.
You have to add option Chrome.exe --allow-file-access-from-files
Firefox allow local file.
<!DOCTYPE html>
<html>
<body>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button><br><br>
<pre id="txt">
Hello
</pre>
<script>
var w;
function startWorker()
{
if(typeof(Worker) !== "undefined")
{
if(typeof(w) == "undefined")
{
w = new Worker("StarForge.js");
}
w.onmessage = function(event)
{
document.getElementById("txt").innerHTML += event.data;
};
}
else
{
document.getElementById("txt").innerHTML = "Sorry! No Web Worker support.";
}
}
function stopWorker()
{
w.terminate();
w = undefined;
}
</script>
</body>
</html>
And in the StarForge.js
function gen()
{
var moon = 2946;
var star = 4125;
var max = 50;
var a,b,c,d,e,f;
var moonx,starx,tokenx;
var token = 0;
var txt;
for (a = 0 ; a <= max ; a++)
{
for (b = 0 ; b <= max ; b++)
{
for (c = 0 ; c <= max ; c++)
{
for (d = 0 ; d <= max ; d++)
{
for (e = 0 ; e <= max ; e++)
{
for (f = 0 ; f <= max ; f++)
{
tokenx = (210 * a) + (235 * b) + (260 * c) + (300 * d) + (375 * e) + (500 * f);
moonx = (10 * a) + (30 * b) + (50 * c) + (75 * d) + (125 * e) + (200 * f);
starx = 75 * (a + b + c + d + e + f);
if (tokenx >= token && moonx <= moon && starx <= star)
{
token = tokenx;
txt = tokenx + ',' + moonx + ',' + starx + ',' + a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f;
postMessage(txt)
console.log(txt);
}
}
}
}
}
}
}
postMessage("--- End ---");
console.log("--- End ---");
}
gen();

How to get String output as in single quote in javascript

Advance thanks, Question looks like simple but i could not able to find the solution.
function sumStrings(a, b)
{
return +a + +b;
}
sumStrings('1','2') //=>3
Expected output
sumStrings('1','2') // => '3'
After addition, add ' to beginning and end.
function sumStrings(a, b){
return "'" + (Number(a) + Number(b)) + "'";
}
console.log(sumStrings('1','2'));
function sumStrings(a, b) {
// Separate decimal part here
var pointa = a.indexOf(".");
var pointb = b.indexOf(".");
var deca = pointa != -1 ? a.substring(pointa + 1) : "0";
var decb = pointb != -1 ? b.substring(pointb + 1) : "0";
if (deca.length < decb.length)
deca += (Math.pow(10, decb.length - deca.length)).toString().substring(1);
else
decb += (Math.pow(10, deca.length - decb.length)).toString().substring(1);
var inta = pointa != -1 ? a.substring(0, pointa) : a;
var intb = pointb != -1 ? b.substring(0, pointb) : b;
// console.log(deca + " " + decb);
var decc = addBigInt(deca, decb);
var intc = addBigInt(inta, intb);
if (decc.length > deca.length) {
intc = addBigInt(intc, "1");
decc = decc.substring(1);
}
var lastZero = decc.length - 1;
while (lastZero >= 0 && decc[lastZero] == "0") {
lastZero--;
}
if (lastZero >= 0)
return intc + "." + decc.substring(0, lastZero + 1);
else
return intc;
}
function addBigInt(a, b) {
var inda = a.length - 1;
var indb = b.length - 1;
var c = [];
const zero = "0".charCodeAt(0);
var carry = 0;
var sum = 0;
while (inda >= 0 && indb >= 0) {
var d1 = a.charCodeAt(inda--) - zero;
var d2 = b.charCodeAt(indb--) - zero;
sum = (d1 + d2 + carry);
carry = Math.floor(sum / 10);
sum %= 10;
c.unshift(sum);
}
if (inda >= 0) {
while (carry && inda >= 0) {
sum = a.charCodeAt(inda--) - zero + carry;
c.unshift(sum % 10);
carry = Math.floor(sum / 10);
}
c.unshift(a.substring(0, inda + !carry));
} else {
while (carry && indb >= 0) {
sum = b.charCodeAt(indb--) - zero + carry;
c.unshift(sum % 10);
carry = Math.floor(sum / 10);
}
c.unshift(b.substring(0, indb + !carry));
}
if (carry)
c.unshift(carry);
return c.join("");
}
console.log(sumStrings("1","2"));
console.log(sumStrings("800","9567"));
console.log(sumStrings("99.1","1"));
console.log(sumStrings("00103","08567"));
console.log(sumStrings("50095301248058391139327916261.5","81055900096023504197206408605"));
If you want to escape the single quote, you can try to add a backslash before the single quote.
i.e.
var x = '\'5\'';
With the new template literal you can try this:
a=`'5'`;
console.log(a);

Mandelbrot set defined by a specific function

I'm experimenting with canvas and I'm trying to modify this piece of code, but unfortunately I don't understand some parts of it.
My question is - how to customize the above code to be defined for example by
f(z) = c^e(-z)
(the formula is taken from a book with fractal examples)?
I know that I need to change this part of code:
function computeRow(task) {
var iter = 0;
var c_i = task.i;
var max_iter = task.max_iter;
var escape = task.escape * task.escape;
task.values = [];
for (var i = 0; i < task.width; i++) {
var c_r = task.r_min + (task.r_max - task.r_min) * i / task.width;
var z_r = 0, z_i = 0;
for (iter = 0; z_r*z_r + z_i*z_i < escape && iter < max_iter; iter++) {
// z -> z^2 + c
var tmp = z_r*z_r - z_i*z_i + c_r;
z_i = 2 * z_r * z_i + c_i;
z_r = tmp;
}
if (iter == max_iter) {
iter = -1;
}
task.values.push(iter);
}
return task;
}
But can't what z_i, z_r, c_i, c_r really means and how I could bind them to the above formula.
Any help would be greatly appreciated.
Complex number have a two part: real, imaginary.
So z = a + b*i, where a is real part, and b*i is imaginary.
In provided sample for z=z^2+c, where z=z_r+z_i*i
NOTE: i*i = -1
So z^2 = (z_r+z_i*i)*(z_r+z_i*i) = z_r*z_r+2*z_r*z_i*i + z_i*i*z_i*i = z_r*z_r+2*z_r*z_i*i - z_i*z_i
now add c: z_r*z_r+2*z_r*z_i*i - z_i*z_i + c_r + c_i*i group it
z_r*z_r+2*z_r*z_i*i - z_i*z_i + c_r + c_i*i = (z_r*z_r - z_i*z_i + c_r) + (2*z_r*z_i + c_i)*i
So we get tmp var from code - is real part of new z
tmp = z_r*z_r - z_i*z_i + c_r
and imaginary part
2*z_r*z_i + c_i
Since z = z_r + z_i * i, we need assign
z_r = z_r*z_r - z_i*z_i + c_r
z_i = 2*z_r*z_i + c_i
UPDATE: for f(z) = e^z - c
first, few complex form: x = a+b*i = |x|(cos(p)+i*sin(p)) = |x|*e^(i*p)
where |x| = sqrt(a*a + b*b) and p = b/a
in our case: p=z_i/z_r, |z| = sqrt(z_r*z_r+z_i*z_i)
e^z = e^(z_r+z_i*i) = e^z_r * (e^z_i*i) = e^z_r * (cos(p)+i*sin(p)) = (e^z_r * cos(p)) + i * (e^z_r * sin(p))
subtract c:
(e^z_r * cos(p)) + i * (e^z_r * sin(p)) - c_r - c_i*i = (e^z_r * cos(p) - c_r) + i * (e^z_r * sin(p) - c_i)
so new z
z_r = (e^z_r * cos(p) - c_r) = (e^z_r * cos(z_i/z_r) - c_r)
z_i = (e^z_r * sin(p) - c_i) = (e^z_r * sin(z_i/z_r) - c_i)

Automatically fire a JavaScript code after loading a page in Google Chrome

I have a Javascript code, which I use to bring Night Mode effect on an HTML page...
The code goes something like this-
javascript: (function () {
function RGBtoHSL(RGBColor) {
with(Math) {
var R, G, B;
var cMax, cMin;
var sum, diff;
var Rdelta, Gdelta, Bdelta;
var H, L, S;
R = RGBColor[0];
G = RGBColor[1];
B = RGBColor[2];
cMax = max(max(R, G), B);
cMin = min(min(R, G), B);
sum = cMax + cMin;
diff = cMax - cMin;
L = sum / 2;
if (cMax == cMin) {
S = 0;
H = 0;
} else {
if (L <= (1 / 2)) S = diff / sum;
else S = diff / (2 - sum);
Rdelta = R / 6 / diff;
Gdelta = G / 6 / diff;
Bdelta = B / 6 / diff;
if (R == cMax) H = Gdelta - Bdelta;
else if (G == cMax) H = (1 / 3) + Bdelta - Rdelta;
else H = (2 / 3) + Rdelta - Gdelta; if (H < 0) H += 1;
if (H > 1) H -= 1;
}
return [H, S, L];
}
}
function getRGBColor(node, prop) {
var rgb = getComputedStyle(node, null).getPropertyValue(prop);
var r, g, b;
if (/rgb\((\d+),\s(\d+),\s(\d+)\)/.exec(rgb)) {
r = parseInt(RegExp.$1, 10);
g = parseInt(RegExp.$2, 10);
b = parseInt(RegExp.$3, 10);
return [r / 255, g / 255, b / 255];
}
return rgb;
}
function hslToCSS(hsl) {
return "hsl(" + Math.round(hsl[0] * 360) + ", " + Math.round(hsl[1] * 100) + "%, " + Math.round(hsl[2] * 100) + "%)";
}
var props = ["color", "background-color", "border-left-color", "border-right-color", "border-top-color", "border-bottom-color"];
var props2 = ["color", "backgroundColor", "borderLeftColor", "borderRightColor", "borderTopColor", "borderBottomColor"];
if (typeof getRGBColor(document.documentElement, "background-color") == "string") document.documentElement.style.backgroundColor = "white";
revl(document.documentElement);
function revl(n) {
var i, x, color, hsl;
if (n.nodeType == Node.ELEMENT_NODE) {
for (i = 0; x = n.childNodes[i]; ++i) revl(x);
for (i = 0; x = props[i]; ++i) {
color = getRGBColor(n, x);
if (typeof (color) != "string") {
hsl = RGBtoHSL(color);
hsl[2] = 1 - hsl[2];
n.style[props2[i]] = hslToCSS(hsl);
}
}
}
}
})()
I have saved this as a Bookmarklet in my Bookmark Bar on Google Chrome, but I want this to be automatically applied to every page I load. What should I do to achieve this?
You should write this as a userscript and run it with something like tampermonkey

Categories

Resources