136 lines
5.3 KiB
Python
136 lines
5.3 KiB
Python
import urllib.request
|
|
import json
|
|
import base64
|
|
import time
|
|
|
|
url_base_kadence = "https://howdoyouconvert.com/wp-json/wp/v2/kadence_element/"
|
|
creds = base64.b64encode(b"ben:6YGf wVxu gBpz pkqx BGZO lfVP").decode("utf-8")
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic " + creds,
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
|
}
|
|
|
|
def patch_element(element_id, title, js_logic):
|
|
print(f"Updating Element {element_id}: {title}")
|
|
js_wrapped = f"""<script>
|
|
window.addEventListener('DOMContentLoaded', (event) => {{
|
|
{js_logic}
|
|
}});
|
|
</script>"""
|
|
|
|
data = {
|
|
"content": js_wrapped
|
|
}
|
|
|
|
url = f"{url_base_kadence}{element_id}"
|
|
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"), headers=headers, method="PUT")
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=30)
|
|
print(f"--> Success")
|
|
except Exception as e:
|
|
print(f"--> Error: {e}")
|
|
if hasattr(e, 'read'): print(e.read().decode('utf-8'))
|
|
|
|
# --- LOGIC BLOCKS ---
|
|
|
|
triple_logic_base = """
|
|
function solve(lastId) {
|
|
let a = parseFloat(document.getElementById("input-1").value);
|
|
let v = parseFloat(document.getElementById("input-2").value);
|
|
let w = parseFloat(document.getElementById("input-3").value);
|
|
|
|
if (isNaN(a) && isNaN(v) && isNaN(w)) return;
|
|
|
|
if (lastId === 'input-1' || lastId === 'input-2') {
|
|
if (!isNaN(a) && !isNaN(v)) {
|
|
let res = (a * v) / [DIVISOR];
|
|
document.getElementById("input-3").value = parseFloat(res.toFixed(3));
|
|
}
|
|
} else if (lastId === 'input-3') {
|
|
if (!isNaN(w)) {
|
|
if (!isNaN(a) && a !== 0) {
|
|
let res = (w * [DIVISOR]) / a;
|
|
document.getElementById("input-2").value = parseFloat(res.toFixed(2));
|
|
} else if (!isNaN(v) && v !== 0) {
|
|
let res = (w * [DIVISOR]) / v;
|
|
document.getElementById("input-1").value = parseFloat(res.toFixed(2));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
document.getElementById("input-1").addEventListener("input", () => solve('input-1'));
|
|
document.getElementById("input-2").addEventListener("input", () => solve('input-2'));
|
|
document.getElementById("input-3").addEventListener("input", () => solve('input-3'));
|
|
"""
|
|
|
|
text_logic_ascii_to_bin = """
|
|
function convert1() {
|
|
let val = document.getElementById("input-1").value;
|
|
let binary = "";
|
|
for (let i = 0; i < val.length; i++) {
|
|
let bin = val[i].charCodeAt(0).toString(2);
|
|
binary += ("00000000" + bin).slice(-8) + " ";
|
|
}
|
|
document.getElementById("input-2").value = binary.trim();
|
|
}
|
|
function convert2() {
|
|
let val = document.getElementById("input-2").value.replace(/\\s/g, "");
|
|
let text = "";
|
|
for (let i = 0; i < val.length; i += 8) {
|
|
let byte = val.substr(i, 8);
|
|
if (byte.length === 8) {
|
|
text += String.fromCharCode(parseInt(byte, 2));
|
|
}
|
|
}
|
|
document.getElementById("input-1").value = text;
|
|
}
|
|
document.getElementById("input-1").addEventListener("input", convert1);
|
|
document.getElementById("input-2").addEventListener("input", convert2);
|
|
"""
|
|
|
|
text_logic_bin_to_dec = """
|
|
function convert1() {
|
|
let val = document.getElementById("input-1").value.replace(/\\s/g, "");
|
|
if (val) document.getElementById("input-2").value = parseInt(val, 2).toString(10);
|
|
else document.getElementById("input-2").value = "";
|
|
}
|
|
function convert2() {
|
|
let val = document.getElementById("input-2").value;
|
|
if (val) document.getElementById("input-1").value = parseInt(val, 10).toString(2);
|
|
else document.getElementById("input-1").value = "";
|
|
}
|
|
document.getElementById("input-1").addEventListener("input", convert1);
|
|
document.getElementById("input-2").addEventListener("input", convert2);
|
|
"""
|
|
|
|
text_logic_bin_to_hex = """
|
|
function convert1() {
|
|
let val = document.getElementById("input-1").value.replace(/\\s/g, "");
|
|
if (val) document.getElementById("input-2").value = parseInt(val, 2).toString(16).toUpperCase();
|
|
else document.getElementById("input-2").value = "";
|
|
}
|
|
function convert2() {
|
|
let val = document.getElementById("input-2").value;
|
|
if (val) document.getElementById("input-1").value = parseInt(val, 16).toString(2);
|
|
else document.getElementById("input-1").value = "";
|
|
}
|
|
document.getElementById("input-1").addEventListener("input", convert1);
|
|
document.getElementById("input-2").addEventListener("input", convert2);
|
|
"""
|
|
|
|
updates = [
|
|
(140, "ASCII to Binary", text_logic_ascii_to_bin),
|
|
(152, "Binary to ASCII", text_logic_ascii_to_bin), # Logic is the same, just order of fields in UI differs? No, inputs are 1 and 2.
|
|
(154, "Binary to Decimal", text_logic_bin_to_dec),
|
|
(156, "Binary to Hex", text_logic_bin_to_hex),
|
|
(142, "Amps to Volts", triple_logic_base.replace("[DIVISOR]", "1")),
|
|
(158, "Amps to Watts", triple_logic_base.replace("[DIVISOR]", "1")),
|
|
(160, "Amps to Kilowatts", triple_logic_base.replace("[DIVISOR]", "1000")),
|
|
(162, "Amps to kVA", triple_logic_base.replace("[DIVISOR]", "1000"))
|
|
]
|
|
|
|
for eid, title, logic in updates:
|
|
patch_element(eid, title, logic)
|
|
time.sleep(1)
|