Skip to content

Instantly share code, notes, and snippets.

@korkey128k
Last active July 29, 2021 10:25
Show Gist options
  • Save korkey128k/bce740d1c235cdaa14d7268eb79ce423 to your computer and use it in GitHub Desktop.
Save korkey128k/bce740d1c235cdaa14d7268eb79ce423 to your computer and use it in GitHub Desktop.

Revisions

  1. korkey128k revised this gist Jul 29, 2021. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name Add Quick Calculations to 1inch
    // @namespace http://tampermonkey.net/
    // @version 0.4
    // @version 0.5
    // @description Add quick links on 1inch, similar to trading interfaces
    // @author Korkey128k
    // @match https://app.1inch.io/
    @@ -23,6 +23,7 @@
    if (event.animationName == "nodeInserted") {
    if(document.querySelector('app-token-amount-input') !== null) {
    maybeAddButtons()
    document.querySelector('[data-id="swap-box.src-token-input"]').addEventListener('keyup', deselectButton)
    }
    }
    }, false);
    @@ -110,8 +111,6 @@
    let percentAmt = calculatePercent(thisPercent)

    fillIn(percentAmt)

    document.querySelector('[data-calc-set]').addEventListener('keyup', deselectButton)
    }

    function deselectButton(event) {
  2. korkey128k revised this gist Jul 27, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@

    writeStylez()

    var inputEvent = new Event('change', { bubbles: true });
    var inputEvent = new Event('keyup', { bubbles: true });
    var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;

    document.addEventListener("animationstart", function(event) {
  3. korkey128k revised this gist Jul 27, 2021. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name Add Quick Calculations to 1inch
    // @namespace http://tampermonkey.net/
    // @version 0.3
    // @version 0.4
    // @description Add quick links on 1inch, similar to trading interfaces
    // @author Korkey128k
    // @match https://app.1inch.io/
    @@ -115,9 +115,6 @@
    }

    function deselectButton(event) {
    console.log(event.target.value)
    console.log(parseFloat(event.target.getAttribute('data-calc-set')))

    if(parseFloat(event.target.getAttribute('data-calc-set')) != parseFloat(event.target.value)) {
    event.target.removeAttribute('data-calc-set')
    event.target.removeEventListener('keyup', deselectButton)
  4. korkey128k revised this gist Jul 27, 2021. 1 changed file with 18 additions and 7 deletions.
    25 changes: 18 additions & 7 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name Add Quick Calculations to 1inch
    // @namespace http://tampermonkey.net/
    // @version 0.2
    // @version 0.3
    // @description Add quick links on 1inch, similar to trading interfaces
    // @author Korkey128k
    // @match https://app.1inch.io/
    @@ -16,7 +16,7 @@

    writeStylez()

    var inputEvent = new Event('keyup', { bubbles: true });
    var inputEvent = new Event('change', { bubbles: true });
    var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;

    document.addEventListener("animationstart", function(event) {
    @@ -105,14 +105,24 @@

    document.querySelectorAll('#quick-calc-container span').forEach((button) => { button.classList.remove('active') });
    event.target.classList.add('active')

    document.querySelector('[data-id="swap-box.src-token-input"]').addEventListener('keyup', (event) => {
    document.querySelectorAll('#quick-calc-container span.active').forEach((button) => { button.classList.remove('active') });
    });

    let thisPercent = parseFloat(event.target.getAttribute('data-percent'))
    let percentAmt = calculatePercent(thisPercent)

    fillIn(percentAmt)

    document.querySelector('[data-calc-set]').addEventListener('keyup', deselectButton)
    }

    function deselectButton(event) {
    console.log(event.target.value)
    console.log(parseFloat(event.target.getAttribute('data-calc-set')))

    fillIn(calculatePercent(thisPercent))
    if(parseFloat(event.target.getAttribute('data-calc-set')) != parseFloat(event.target.value)) {
    event.target.removeAttribute('data-calc-set')
    event.target.removeEventListener('keyup', deselectButton)
    document.querySelectorAll('#quick-calc-container span.active').forEach((button) => { button.classList.remove('active') });
    }
    }

    function calculatePercent(percent) {
    @@ -130,6 +140,7 @@
    function fillIn(amt) {
    let tokenInput = document.querySelector('[data-id="swap-box.src-token-input"]')
    tokenInput.value = amt
    tokenInput.setAttribute('data-calc-set', amt)
    tokenInput.dispatchEvent(inputEvent)
    }
    })();
  5. korkey128k revised this gist Jul 27, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name Add Quick Calculations to 1inch
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @version 0.2
    // @description Add quick links on 1inch, similar to trading interfaces
    // @author Korkey128k
    // @match https://app.1inch.io/
  6. korkey128k revised this gist Jul 27, 2021. 1 changed file with 24 additions and 2 deletions.
    26 changes: 24 additions & 2 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,10 @@
    animation-duration: 0.001s;
    animation-name: nodeInserted;
    }
    .field, .field:focus {
    border-radius: 16px 16px 0 0 !important;
    }
    #quick-calc-container {
    display: flex;
    @@ -48,8 +52,10 @@
    align-items: stretch;
    width: 100%;
    background: rgb(33, 36, 41);
    padding: 0.6em 0 1em;
    background: rgb(6, 10, 16);
    padding: 0.6em 0.7em 1em;
    border-radius: 0 0 16px 16px;
    font-size: 0.9em;
    }
    #quick-calc-container span {
    @@ -59,6 +65,15 @@
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    border-radius: 7px;
    }
    #quick-calc-container span:hover {
    background-color: #3e3e3e;
    }
    #quick-calc-container span.active {
    background-color: #5b5b5b;
    }
    `

    @@ -87,6 +102,13 @@
    function modifyInput(event) {
    event.preventDefault();
    event.stopPropagation();

    document.querySelectorAll('#quick-calc-container span').forEach((button) => { button.classList.remove('active') });
    event.target.classList.add('active')

    document.querySelector('[data-id="swap-box.src-token-input"]').addEventListener('keyup', (event) => {
    document.querySelectorAll('#quick-calc-container span.active').forEach((button) => { button.classList.remove('active') });
    });

    let thisPercent = parseFloat(event.target.getAttribute('data-percent'))

  7. korkey128k revised this gist Jul 27, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@
    // @match https://app.1inch.io/
    // @icon https://www.google.com/s2/favicons?domain=1inch.io
    // @grant none
    // @downloadURL https://gist.github.com/Korkey128k/3806feb286f89659b88c8d53ff39b2c5/raw
    // @updateURL https://gist.github.com/Korkey128k/3806feb286f89659b88c8d53ff39b2c5/raw
    // @downloadURL https://gist.github.com/Korkey128k/bce740d1c235cdaa14d7268eb79ce423/raw
    // @updateURL https://gist.github.com/Korkey128k/bce740d1c235cdaa14d7268eb79ce423/raw
    // ==/UserScript==

    (function() {
  8. korkey128k created this gist Jul 27, 2021.
    113 changes: 113 additions & 0 deletions 1inch-quick-calc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    // ==UserScript==
    // @name Add Quick Calculations to 1inch
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @description Add quick links on 1inch, similar to trading interfaces
    // @author Korkey128k
    // @match https://app.1inch.io/
    // @icon https://www.google.com/s2/favicons?domain=1inch.io
    // @grant none
    // @downloadURL https://gist.github.com/Korkey128k/3806feb286f89659b88c8d53ff39b2c5/raw
    // @updateURL https://gist.github.com/Korkey128k/3806feb286f89659b88c8d53ff39b2c5/raw
    // ==/UserScript==

    (function() {
    'use strict';

    writeStylez()

    var inputEvent = new Event('keyup', { bubbles: true });
    var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;

    document.addEventListener("animationstart", function(event) {
    if (event.animationName == "nodeInserted") {
    if(document.querySelector('app-token-amount-input') !== null) {
    maybeAddButtons()
    }
    }
    }, false);

    function writeStylez() {
    const stylez = document.createElement('style')

    stylez.innerHTML = `
    @keyframes nodeInserted {
    from { opacity: 0.99; }
    to { opacity: 1; }
    }
    app-token-amount-input {
    animation-duration: 0.001s;
    animation-name: nodeInserted;
    }
    #quick-calc-container {
    display: flex;
    flex-flow: row nowrap;
    justify-content: center;
    align-items: stretch;
    width: 100%;
    background: rgb(33, 36, 41);
    padding: 0.6em 0 1em;
    }
    #quick-calc-container span {
    flex: 1 1 auto;
    padding: 0.5em 0;
    color: white;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    }
    `

    document.head.appendChild(stylez);

    }

    function maybeAddButtons() {
    if(document.querySelector('#quick-calc-container') === null) {
    const buttonContainer = document.createElement('div')
    buttonContainer.id = 'quick-calc-container'

    for(const percent of [5, 10, 15, 20, 25, 30, 40, 50, 60, 75, 90]) {
    let button = document.createElement('span')
    button.setAttribute('data-percent', percent)
    button.innerText = `${percent}%`

    buttonContainer.appendChild(button)
    }

    document.querySelector('app-token-amount-input').appendChild(buttonContainer)
    document.querySelectorAll('#quick-calc-container span').forEach((button) => button.addEventListener('click', modifyInput) )
    }
    }

    function modifyInput(event) {
    event.preventDefault();
    event.stopPropagation();

    let thisPercent = parseFloat(event.target.getAttribute('data-percent'))

    fillIn(calculatePercent(thisPercent))
    }

    function calculatePercent(percent) {

    // Lolz at the selector. Fra gil ey, must be french...
    let balanceElem = document.querySelector('[data-id="swap-box.balance-set-max"]')

    if(balanceElem.textContent.match(/\d+(\.\d+)?/g) !== null) {
    let balance = balanceElem.textContent.match(/\d+(\.\d+)?/g).map((match) => { return parseFloat(match) } )[0]
    return (percent / 100.0) * balance
    }

    }

    function fillIn(amt) {
    let tokenInput = document.querySelector('[data-id="swap-box.src-token-input"]')
    tokenInput.value = amt
    tokenInput.dispatchEvent(inputEvent)
    }
    })();