Hive Power Calculator
[English] - język polski poniżej
Hello next day!
Let's start with the first code, which was supposed to appear in the welcome post, but I think too many bytes prevented me from posting it. So let's get to the meat of it, comments will be in the code (any questions for further explanation in the comments). I'll try to alternate between basic topics and more advanced/multithreaded topics.
This is a simple plugin/inset for CodeInjector that was created one day when I wanted to summarize someone's rewards in that person's account history.
// We are creating a div element in which it will serve us as the body of the counter
const resultDiv = document.createElement("div");
resultDiv.style.position = "fixed";
resultDiv.style.top = "100px";
resultDiv.style.right = "10px";
resultDiv.style.backgroundColor = "#ff0";
resultDiv.style.border = "1px solid #000";
resultDiv.style.padding = "5px";
document.body.appendChild(resultDiv);
// We are creating a function that will be executed every second to monitor the selection and display the results.
function checkSelection() {
// Fetching selected text
const selectedText = window.getSelection().toString();
// Regular expression that will match decimal numbers along with the unit "HIVE POWER".
const regex = /Claim rewards:\s+(\d+\.\d+)\s+HIVE POWER/g;
// Sum numeric values found in selected text
let sum = 0;
let match;
while (match = regex.exec(selectedText)) {
// Removing the "Claim rewards: " text and the " HIVE POWER" unit from the found match
const value = match[1];
// Parsing a number as a float value and adding it to the sum
sum += parseFloat(value);
}
// Displaying a calculated numeric value in a div
resultDiv.textContent = sum;
// Set the value of the page's title tag to a calculated value
document.title = `${sum} HIVE`;
}
// Call checkSelection function in every second
setInterval(checkSelection, 1000);
Short video of how the code should work:
I hope someone finds the code useful and encourages them to continue coding. In the near future, projects will be uploaded to github for community development.
For code usage, we can use the Code Injector plugin available in the chrome/brave extensions database
What is CodeInjector?
It is a browser plugin that allows injecting almost any JavaScript/HTML/CSS code onto a specified website, enabling customization of its resources or additional automation.
[Polish] Witam dnia następnego!
Polecimy z pierwszym kodem, który miał pojawić się już w poście powitalnym jednak chyba zbyt duża ilość bajtów uniemożliwiła mi postowanie. Przejdźmy więc do samego mięsa, komentarze będą w kodzie (ewentualne pytania o rozwinięcie w komentarzach). Postaram się na przemian poruszać tematy banalne (podstawy) jak i tematy bardziej zaawansowane/wielowątkowe.
Jest to prosta wtyczka/wklejka do CodeInjectora, która powstała pewnego dnia gdy chciałem podsumować czyjeś nagrody w historii konta tej osoby.
// Tworzymy element div w którym będzie służył nam jako ciało licznika
const resultDiv = document.createElement("div");
resultDiv.style.position = "fixed";
resultDiv.style.top = "100px";
resultDiv.style.right = "10px";
resultDiv.style.backgroundColor = "#ff0";
resultDiv.style.border = "1px solid #000";
resultDiv.style.padding = "5px";
document.body.appendChild(resultDiv);
// Tworzymy funkcję, która będzie wykonywana co sekundę, w celu monitorowania zaznaczenie i wyświetlania wyników
function checkSelection() {
// Pobieranie zaznaczonego tekstu z dokumentu
const selectedText = window.getSelection().toString();
// Wyrażenie regularne, które dopasuje liczby dziesiętne wraz z jednostką "HIVE POWER"
const regex = /Claim rewards:\s+(\d+\.\d+)\s+HIVE POWER/g;
// Sumowanie wartości liczbowych z fraz znalezionych w zaznaczonym tekście
let sum = 0;
let match;
while (match = regex.exec(selectedText)) {
// Usunięcie tekstu "Claim rewards: " oraz jednostki " HIVE POWER" ze znalezionego dopasowania
const value = match[1];
// Parsowanie liczby jako wartość zmiennoprzecinkowej i dodanie jej do sumy
sum += parseFloat(value);
}
// Wyświetlenie zliczonej wartości liczbowej w elemencie div
resultDiv.textContent = sum;
// Ustawienie wartości tagu title strony na zliczoną wartość
document.title = `${sum} HIVE`;
}
// Wywołanie funkcji checkSelection co sekundę
setInterval(checkSelection, 1000);
krótki film z tego jak powinien działać kod:
Mam nadzieję, że kod się komuś przyda i zachęci do dalszego kodowania. W niedalekiej przyszłości projekty będą umieszczane na githubie w celu umożliwienia ich rozwoju przez społeczność.
Na potrzeby użycia kodu możemy użyć wtyczki Code Injector dostępnej w bazie rozszerzeń chrome/brave
Czym jest CodeInjector?
Jest to wtyczka do przeglądarki, która pozwala wstrzyknąć niemalże dowolny kod javascript/html/css na określonej stronie co umożliwia
własną personalizację zasobów tej strony lub np. dodatkową automatyzację.
Enjoy!