This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Отправляет сообщение в контент скрипт. | |
* Вызвается из обработчика (например клик по кнопке), на вход принимает событие evt из которого получаем evt.target.id | |
* Т.е. индентифицируем кнопку на которой произошло событие и отправляем и ее id в качестве темы сообщения | |
* В content.js соответсвенно добавляем слушатель: | |
* chrome.runtime.onMessage.addListener((msg, sender, response) => { // тут код if (msg.subject === ..) }); | |
* @param {event} evt | |
* @return {Promise<void>} | |
*/ | |
let sendMsgToContent = async function (evt) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Функция переименовывает ключи переданного ей объекта | |
* См. http://qaru.site/questions/41346/javascript-object-rename-key | |
* | |
* ES6 может сделать это в одну строку: delete Object.assign(o, {[newKey]: o[oldKey] })[oldKey]; | |
* Или две строки, если вы хотите создать новый объект: | |
* const newObject = {}; | |
* delete Object.assign(newObject, o, {[newKey]: o[oldKey] })[oldKey]; | |
* | |
* @param {object} obj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
async function something() { | |
console.log("this might take some time...."); | |
await delay(5000); | |
console.log("done!") | |
} | |
something(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Запускать функцию с рендомным интервалом, в заданном диапазоне - упрощенный вариант | |
* https://stackoverflow.com/questions/34656758/javascript-setinterval-with-random-time?rq=1 | |
*/ | |
function myFunction() { | |
alert(`oo`); | |
setTimeout(myFunction, Math.random() * 5000); | |
} | |
myFunction(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Запускать функцию с рендомным интервалом, в заданном диапазоне | |
* https://stackoverflow.com/questions/34656758/javascript-setinterval-with-random-time?rq=1 | |
*/ | |
function myFunction() { | |
const min = 5; | |
const max = 10; | |
let rand = Math.floor(Math.random() * (max - min + 1) + min); // Generate Random number between 5 - 10 | |
alert(`Wait for ` + rand + ` seconds`); | |
setTimeout(myFunction, rand * 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Вызывает переданную ей функцюи с задержкой | |
* https://learn.javascript.ru/task/delay | |
* @param f | |
* @param ms | |
* @return {Function} | |
*/ | |
export const delay = function (f, ms) { | |
return function () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Возвращает ключ объекта по его значению. Нет функционала в js ? | |
* Вернет только первый ключ, если одинаковых значений несколько? | |
* @param {object} obj | |
* @param {any} value | |
* @return {string | undefined} | |
*/ | |
const getKeyByValue = (obj, value) => | |
Object.keys(obj).find((key) => obj[key] === value); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<button> | |
Click me! → And see console! | |
</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Функция возвращает массив с уникальными элементами | |
* Только в ES6 https://webformyself.com/kak-proizvesti-udalenie-dublej-massiva-v-es6/ | |
* @param {array} arr | |
* @return {any[]} | |
*/ | |
function returnUniqueArray(arr) { | |
return Array.from(new Set(arr)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Функция возвращает массив со случайно перемешанными элементами | |
* https://habr.com/ru/post/358094/ | |
* @param {array} arr | |
* @return {array} | |
*/ | |
function shuffle(arr){ | |
let j, temp; | |
for(let i = arr.length - 1; i > 0; i--){ | |
j = Math.floor(Math.random()*(i + 1)); |