VS Code, virtualenvironment and Django; simple setup

Just found out that VS code is genious:

in shell, make a project folder:
>mkdir myproject
>cd myproject
>python3 -m virtualenv myproject_venv
>source myproject_venv/bin/activate
>pip install Django
>django-admin startproject myproject .Code language: Bash (bash)

Now if you open the “root” myproject folder, the one created on line 1, VS code will detect the django project and the virtual environment. So hit F5 to to run the server in debug mode.

waitForAsyncElement()

Simple function to run javascript after specific content is loaded.
jQuery dependant.
Useful for dealing with resizing iframes after content is loaded to avoid scroll bars in the iframe. Also serves as an alternative to waitForKeyElement when tinkering with userscripts et.

Doc:

/*
Usage:
waitForAsyncElement(selector, code, delay, timeout, fallback);

Breakdown:
    selector: jQuery selector as string: "#yourElementHere"
    code:     function with code to run: function(){"code here"};
    delay:    delay in ms after element is loaded: default 30ms
    timeout:  stop execution if element is not loading: default 5000ms
    fallback: if timed out run fallback, example: function(){alert("async did not load")")}

Example:
*/
waitForAsyncElement("#some-data", function(){
      console.log($("#some-data").val());
})
//Example with fallback:
waitForAsyncElement("#some-data", function(){
      console.log($("#some-data").val());
}, 30, 5000, function(){
      console.log("#some-data did not load");
})

/*
Example for dealing with resizing iFrame after its loaded:
Note the extra 300ms delay to wait for iframe content to load,
iFrame.onload could have been used instead, but seem to fail on mobile.
*/
onElementLoaded("#iFrame1", function(){
        var iFrame = document.getElementById('iFrame1');
        iFrame.height = iFrame.contentWindow.document.body.scrollHeight + 100;
}, 300);Code language: JavaScript (javascript)

Code:

var waitForAsyncElement = function(elementSelector, 
                               callback, 
                               delay = 30, 
                               timeoutms = 5000, 
                               timeoutFallback = function(){return}){
    var intervl = setInterval(function(){
        if (jQuery(elementSelector).length){
            clearInterval(intervl);
            setTimeout(() => {callback()}, delay);
        }
    }, 300);
    if (timeoutms !== 0){
        setTimeout(() => {
            clearInterval(intervl);
            timeoutFallback();
        }, timeoutms);
        
    }
};Code language: JavaScript (javascript)

Licence:

Public Domain or MIT, whichever suits you best. However please keep me in the loop if you build on the function, make and improvements and such 🙂

Save and load more reliably with in userscript, maybe.

So, been messing around with userscript lately, in tampermonkey. And hence had a few problems saving and loading with GM_setValue and GM_getValue. There are a couple of scrips out there to sort this for you, but none that i was able to get to work. So I’ve wrapped my own version up, which also compresses via LZstring. (google it).

d.save(“float”, 12.34);
d.load(“float”);

Continue reading

mirror website with wget

wget -r -np -k -nc --execute="robots = off" --wait=5 -E <url>

wget
-r: recursive
-np: no parent directories
-k: convert links to local
-nc: no clobber, skip if file already downloaded
–execute=”robots = off”: ignore robots.txt
–wait=5: wait in between requests
-E: add .html to the end of filenames
<url>

How to automatically stop charging at 80%

TLDR; It’s a bit of work, and you’d like to have some experience with electronics and Arduino. Basically I monitor charging current (or speed if you like) to determine when the charge on my iPhone battery has reached about 80%, and then cut power to the charger.

Note that on Android there are apps that will do this for you.

Also note that on iPhone you can use Automation in the Shortcuts app to send you a notification at a certain charge, and then take the phone off manually.

That said the method I use here could in theory be used to modify any device that uses lithium batteries. I must admit I did it mostly for the challenge, however I also find it useful.

Continue reading