Skip to content

Instantly share code, notes, and snippets.

@iskigow
Last active October 21, 2022 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iskigow/3931c349e9d6580c3302942a655b5bbc to your computer and use it in GitHub Desktop.
Save iskigow/3931c349e9d6580c3302942a655b5bbc to your computer and use it in GitHub Desktop.
Utilitários em botões favoritos do navegador
/*
* Gera um CNPJ no iput focado ou adiciona no final do texto em um textArea
* - Se deseja que seja gerado com mascara deixar a chamada como cnpj(true) no final do script;
* - Se deseja que seja gerado sem mascara deixar a chamada como cnpj(false) no final do script;
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const input = document.activeElement;
let isTextArea = false;
if (
!input ||
!(
(isTextArea = input.nodeName == "TEXTAREA") ||
(input.nodeName == "INPUT" && ["search", "text"].includes(input.type))
)
) {
alert("Selecione um inputText/textArea para atribuir o cpf");
return;
}
const mod = (dividendo, divisor) =>
Math.round(dividendo - Math.floor(dividendo / divisor) * divisor);
const randomiza = (n) => Math.round(Math.random() * n);
const cria_array = (total, n) => Array.from(Array(total), () => randomiza(n));
randomiza(100);
const cnpj = (comPontos) => {
const [n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12] = [
...cria_array(8, 9),
0,
0,
0,
randomiza(8) + 1,
];
let d1 =
n12 * 2 +
n11 * 3 +
n10 * 4 +
n9 * 5 +
n8 * 6 +
n7 * 7 +
n6 * 8 +
n5 * 9 +
n4 * 2 +
n3 * 3 +
n2 * 4 +
n1 * 5;
d1 = 11 - mod(d1, 11);
if (d1 >= 10) d1 = 0;
let d2 =
d1 * 2 +
n12 * 3 +
n11 * 4 +
n10 * 5 +
n9 * 6 +
n8 * 7 +
n7 * 8 +
n6 * 9 +
n5 * 2 +
n4 * 3 +
n3 * 4 +
n2 * 5 +
n1 * 6;
d2 = 11 - mod(d2, 11);
if (d2 >= 10) d2 = 0;
return comPontos
? `${n1}${n2}.${n3}${n4}${n5}.${n6}${n7}${n8}/${n9}${n10}${n11}${n12}-${d1}${d2}`
: `${n1}${n2}${n3}${n4}${n5}${n6}${n7}${n8}${n9}${n10}${n11}${n12}${d1}${d2}`;
};
input.value = (isTextArea ? input.value + " " : "") + cnpj(true);
})();
/*
* Gera um CNPJ e tenta copiar para a área de transferência
* - Se deseja que seja gerado com mascara deixar a chamada como cnpj(true) no final do script;
* - Se deseja que seja gerado sem mascara deixar a chamada como cnpj(false) no final do script;
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const fallbackCopyTextToClipboard = (text) => {
const textArea = document.createElement("textarea");
textArea.style.position = "fixed";
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = 0;
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
textArea.style.background = "transparent";
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand("copy");
var msg = successful ? "successful" : "unsuccessful";
console.log("Copying to clipboard was successful");
} catch (err) {
alert("Oops, unable to copy");
console.error("Could not copy: ", err);
}
document.body.removeChild(textArea);
};
const copyTextToClipboard = (text) => {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(
() => {
console.log("Async: Copying to clipboard was successful!");
},
(err) => {
alert("Oops, unable to copy");
console.error("Async: Could not copy: ", err);
}
);
};
const mod = (dividendo, divisor) =>
Math.round(dividendo - Math.floor(dividendo / divisor) * divisor);
const randomiza = (n) => Math.round(Math.random() * n);
const cria_array = (total, n) => Array.from(Array(total), () => randomiza(n));
randomiza(100);
const cnpj = (comPontos) => {
const [n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12] = [
...cria_array(8, 9),
0,
0,
0,
randomiza(8) + 1,
];
let d1 =
n12 * 2 +
n11 * 3 +
n10 * 4 +
n9 * 5 +
n8 * 6 +
n7 * 7 +
n6 * 8 +
n5 * 9 +
n4 * 2 +
n3 * 3 +
n2 * 4 +
n1 * 5;
d1 = 11 - mod(d1, 11);
if (d1 >= 10) d1 = 0;
let d2 =
d1 * 2 +
n12 * 3 +
n11 * 4 +
n10 * 5 +
n9 * 6 +
n8 * 7 +
n7 * 8 +
n6 * 9 +
n5 * 2 +
n4 * 3 +
n3 * 4 +
n2 * 5 +
n1 * 6;
d2 = 11 - mod(d2, 11);
if (d2 >= 10) d2 = 0;
return comPontos
? `${n1}${n2}.${n3}${n4}${n5}.${n6}${n7}${n8}/${n9}${n10}${n11}${n12}-${d1}${d2}`
: `${n1}${n2}${n3}${n4}${n5}${n6}${n7}${n8}${n9}${n10}${n11}${n12}${d1}${d2}`;
};
copyTextToClipboard(cnpj(true));
})();
/*
* Gera um CPF no iput focado ou adiciona no final do texto em um textArea
* - Se deseja que seja gerado com mascara deixar a chamada como cpf(true) no final do script;
* - Se deseja que seja gerado sem mascara deixar a chamada como cpf(false) no final do script;
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const fallbackCopyTextToClipboard = (text) => {
const textArea = document.createElement("textarea");
textArea.style.position = "fixed";
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = 0;
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
textArea.style.background = "transparent";
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand("copy");
var msg = successful ? "successful" : "unsuccessful";
console.log("Copying to clipboard was successful");
} catch (err) {
alert("Oops, unable to copy");
console.error("Could not copy: ", err);
}
document.body.removeChild(textArea);
};
const copyTextToClipboard = (text) => {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(
() => {
console.log("Async: Copying to clipboard was successful!");
},
(err) => {
alert("Oops, unable to copy");
console.error("Async: Could not copy: ", err);
}
);
};
const mod = (dividendo, divisor) =>
Math.round(dividendo - Math.floor(dividendo / divisor) * divisor);
const randomiza = (n) => Math.round(Math.random() * n);
const cria_array = (total, n) => Array.from(Array(total), () => randomiza(n));
randomiza(100);
const cpf = (comPontos) => {
let [n1, n2, n3, n4, n5, n6, n7, n8, n9] = cria_array(9, 9);
let d1 =
n9 * 2 +
n8 * 3 +
n7 * 4 +
n6 * 5 +
n5 * 6 +
n4 * 7 +
n3 * 8 +
n2 * 9 +
n1 * 10;
d1 = 11 - mod(d1, 11);
if (d1 >= 10) d1 = 0;
let d2 =
d1 * 2 +
n9 * 3 +
n8 * 4 +
n7 * 5 +
n6 * 6 +
n5 * 7 +
n4 * 8 +
n3 * 9 +
n2 * 10 +
n1 * 11;
d2 = 11 - mod(d2, 11);
if (d2 >= 10) d2 = 0;
return comPontos
? `${n1}${n2}${n3}.${n4}${n5}${n6}.${n7}${n8}${n9}-${d1}${d2}`
: `${n1}${n2}${n3}${n4}${n5}${n6}${n7}${n8}${n9}${d1}${d2}`;
};
copyTextToClipboard(cpf(true));
})();
/*
* Gera um CPF e tenta copiar para a área de transferência
* - Se deseja que seja gerado com mascara deixar a chamada como cpf(true) no final do script;
* - Se deseja que seja gerado sem mascara deixar a chamada como cpf(false) no final do script;
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => { const fallbackCopyTextToClipboard = (text) => { const textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = 0; textArea.style.left = 0; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = 0; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Copying to clipboard was successful'); } catch (err) { alert('Oops, unable to copy'); console.error('Could not copy: ', err); } document.body.removeChild(textArea); }; const copyTextToClipboard = (text) => { if (!navigator.clipboard) { fallbackCopyTextToClipboard(text); return; } navigator.clipboard.writeText(text).then(() => { console.log('Async: Copying to clipboard was successful!'); }, (err) => { alert('Oops, unable to copy'); console.error('Async: Could not copy: ', err); }); }; const mod = (dividendo, divisor) => Math.round(dividendo - (Math.floor(dividendo / divisor) * divisor)); const randomiza = (n) => (Math.round(Math.random() * n)); const cria_array = (total, n) => Array.from(Array(total), () => randomiza(n)); randomiza(100); const cpf = (comPontos) => { let [n1, n2, n3, n4, n5, n6, n7, n8, n9] = cria_array(9, 9); let d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10; d1 = 11 - (mod(d1, 11)); if (d1 >= 10) d1 = 0; let d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11; d2 = 11 - (mod(d2, 11)); if (d2 >= 10) d2 = 0; return comPontos ? `${n1}${n2}${n3}.${n4}${n5}${n6}.${n7}${n8}${n9}-${d1}${d2}` : `${n1}${n2}${n3}${n4}${n5}${n6}${n7}${n8}${n9}${d1}${d2}`; }; copyTextToClipboard(cpf(true)); })();
/*
* Pergunta quantos CPFs é desejado e gera a quantidade solicitada de CPFs
* e tenta copiar para a área de transferência.
* - Se deseja que seja gerado com mascara deixar a chamada como cpf(true) no final do script;
* - Se deseja que seja gerado sem mascara deixar a chamada como cpf(false) no final do script;
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const fallbackCopyTextToClipboard = (text) => {
const textArea = document.createElement("textarea");
textArea.style.position = "fixed";
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = 0;
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
textArea.style.background = "transparent";
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand("copy");
var msg = successful ? "successful" : "unsuccessful";
console.log("Copying to clipboard was successful");
} catch (err) {
alert("Oops, unable to copy");
console.error("Could not copy: ", err);
}
document.body.removeChild(textArea);
};
const copyTextToClipboard = (text) => {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(
() => {
console.log("Async: Copying to clipboard was successful!");
},
(err) => {
alert("Oops, unable to copy");
console.error("Async: Could not copy: ", err);
}
);
};
const mod = (dividendo, divisor) =>
Math.round(dividendo - Math.floor(dividendo / divisor) * divisor);
const randomiza = (n) => Math.round(Math.random() * n);
const cria_array = (total, n) => Array.from(Array(total), () => randomiza(n));
randomiza(100);
const cpf = (comPontos) => {
let [n1, n2, n3, n4, n5, n6, n7, n8, n9] = cria_array(9, 9);
let d1 =
n9 * 2 +
n8 * 3 +
n7 * 4 +
n6 * 5 +
n5 * 6 +
n4 * 7 +
n3 * 8 +
n2 * 9 +
n1 * 10;
d1 = 11 - mod(d1, 11);
if (d1 >= 10) d1 = 0;
let d2 =
d1 * 2 +
n9 * 3 +
n8 * 4 +
n7 * 5 +
n6 * 6 +
n5 * 7 +
n4 * 8 +
n3 * 9 +
n2 * 10 +
n1 * 11;
d2 = 11 - mod(d2, 11);
if (d2 >= 10) d2 = 0;
return comPontos
? `${n1}${n2}${n3}.${n4}${n5}${n6}.${n7}${n8}${n9}-${d1}${d2}`
: `${n1}${n2}${n3}${n4}${n5}${n6}${n7}${n8}${n9}${d1}${d2}`;
};
const qtd = +prompt("Quantidade de CPF(s)", "1");
if (Number.isNaN(qtd) || qtd < 1) {
alert("Não foi possível gerar");
return;
}
copyTextToClipboard(
Array(qtd)
.fill("")
.map(() => cpf(true))
.join("\n")
);
})();
/*
* Emite um alerta com o texto selecionado e informa o tamanho
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const getSelectionText = () => {
let text = "";
const activeEl = document.activeElement;
const activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if (
activeElTagName == "textarea" ||
(activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type) &&
typeof activeEl.selectionStart == "number")
) {
text = activeEl.value.slice(
activeEl.selectionStart,
activeEl.selectionEnd
);
} else if (window.getSelection) {
text = window.getSelection().toString();
}
return text;
};
const selectedText = getSelectionText();
alert(`O texto: '${selectedText}'\nPossui tamanho: ${selectedText.length}`);
})();
/*
* Substitui a tag mrkdwn para outra estrutura (html) aceitavel pelo Targhetprocess com borda
* Ex.:
* DE: ![image.png](https://md5.tpondemand.com/Attachment.aspx?AttachmentID=666)
* PARA: <img src="https://md5.tpondemand.com/Attachment.aspx?AttachmentID=666" style="border: 1px solid black" alt="image.png"/>
*
* Faz isso apenas em inputs e textAreas, no caso de seleção de texto comum emite um alerta informando o valor a mudar;
* Caso não aconteça nada normalmente não foi detectada seleção ou a seleção não casa com o formato esperado. No console do navegador
* pode aparecer 'No Selection' ou 'No Match'.
* OBS.: Use apenas o código no botão de favoritos
*/
javascript: (() => {
const IMG_MRKD_REGEX = /^![\[](.+)[\]][(](.+)[)]$/;
const getSelectionText = () => {
const activeEl = document.activeElement;
const activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if (
activeElTagName == "textarea" ||
(activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type) &&
typeof activeEl.selectionStart == "number")
) {
const text = activeEl.value.slice(
activeEl.selectionStart,
activeEl.selectionEnd
);
return {
element: activeEl,
startPos: activeEl.selectionStart,
endPos: activeEl.selectionEnd,
text,
};
} else if (window.getSelection) {
const text = window.getSelection().toString();
return { text };
}
return undefined;
};
const selectedText = getSelectionText();
if (!selectedText || !selectedText.text) {
console.debug("No Selection");
return;
}
if (!selectedText.text.match(IMG_MRKD_REGEX)) {
console.debug("No Match");
return;
}
const replacedText = selectedText.text.replace(
IMG_MRKD_REGEX,
'<img src="$2" style="border: 1px solid black" alt="$1"/>'
);
if (selectedText?.element) {
const element = selectedText.element;
element.value =
element.value.slice(0, selectedText.startPos) +
replacedText +
element.value.slice(selectedText.endPos);
const newEndPosition = selectedText.endPos - selectedText.text.length + replacedText.length;
if (element.createTextRange) {
console.debug('By createTextRange');
let range = element.createTextRange();
range.move('character', newEndPosition);
range.select();
} else if (element.selectionStart) {
console.debug('By selectionStart');
element.focus();
element.setSelectionRange(newEndPosition, newEndPosition);
}
} else {
alert(
`TODO replace\n\nfrom: '${selectedText.text}'\n\nto:'${replacedText}'`
);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment