User Tools

Site Tools


new_window_from_javascript

the javascript stores the links that needs to be open in a js local database and the python scripts polls the databases every 250ms for links that need to be open and cleans up the databases.

It's a hack but it works.

The javascript part:

var db = openDatabase("LinksFollow", "1.0");
var dbOK = false;
if (!db)
    alert("Failed to open the database on disk.  This is probably because the version was bad or there is not enough space left in this domain's quota");
db.transaction(function(tx) {
        tx.executeSql("SELECT COUNT(*) FROM Links", [], function(result) {
                dbready();
            }, function(tx, error) {
                tx.executeSql("CREATE TABLE Links (url TEXT, timestamp REAL)", [], function(result) {
                        dbready();
                    });
            });
    });

function dbready(){
    dbOk = true;
}

function newlink(link){
    if(!dbOk)
        alert("Database not ready!");
    db.transaction(function (tx)
                   {
                       tx.executeSql("INSERT INTO Links (url,timestamp) VALUES (?,?)", [link, new Date().getTime()]);
                   });
}

Python script:

import sqlite3
import sys
import os
import time

base_path = os.environ['XDG_DATA_HOME']
webkit_databases = base_path + "/webkit/databases"
main_datbase = webkit_databases + "/Databases.db"

browsercommand = ['/usr/bin/uzbl','-u']


#CREATE TABLE Databases (guid INTEGER PRIMARY KEY AUTOINCREMENT, origin TEXT, name TEXT, displayName TEXT, estimatedSize INTEGER, path TEXT);
while True:
    conn = sqlite3.connect(main_datbase)
    databases = conn.execute("SELECT origin,path FROM Databases WHERE name = 'LinksFollow'")
    for database in databases:
        path , name = database
        database = "%s/%s/%s" % (webkit_databases,path,name)
        if not os.path.exists(database):
            continue
        try:
            current = sqlite3.connect(database)
            links = current.execute("select * from Links")
            for link in links:
                cmd = browsercommand + [link[0]]
                os.spawnl(os.P_NOWAIT, cmd[0], * cmd)
                os.unlink(database)
        except:
            print "Can't connect to %s" % database
    conn.execute("DELETE FROM Databases WHERE name = 'LinksFollow'")
    conn.commit()
    conn.close()
    del conn
    time.sleep(0.250)
new_window_from_javascript.txt · Last modified: 2016/08/31 14:10 (external edit)