From efb95b52aa5c61e3b749cd732460bc1eb22e9205 Mon Sep 17 00:00:00 2001 From: Kai Hendry Date: Sun, 12 Aug 2012 14:06:36 +0200 Subject: [PATCH] Use grep.php if there is no Application cache or it's slowly syncing TODO: Make URL ?queries work offline! --- create.cgi | 1 + main.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/create.cgi b/create.cgi index 77b94ca..fc029cd 100755 --- a/create.cgi +++ b/create.cgi @@ -66,6 +66,7 @@ fi cd u/$id ln -sf ../../index.html || true +ln -sf ../../grep.php || true if echo $id | grep -q -v '_' # Underscores in domain names is a no no then diff --git a/main.js b/main.js index e8787dc..baf2e29 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,4 @@ var l = []; // avoid expensive $.get for local searches - function search(query, lines) { var results = "

Searched for: " + query + "

    "; for (var i = 0; i < lines.length; i++) { @@ -22,15 +21,31 @@ function search(query, lines) { function grep(query) { - if (l.length > 0) { - search(query, l); - } else { - $.get('tweets.txt', function(data) { - l = data.split("\n"); - search(query, l); - }); - } + if (typeof applicationCache !== 'undefined' && applicationCache.status == 1) { + // If we have a cache, lets do this locally + console.log("Using applicationCache"); + if (l.length > 0) { + search(query, l); + } else { + $.get('tweets.txt', function(data) { + l = data.split("\n"); + search(query, l); + }); + } + + } else { + + // Client doesn't support appcache or it's not in sync, so lets search on the server + console.log("Using grep.php"); + $.getJSON("/u/" + NAME + "/grep.php?jsoncallback=?", { + q: query + }, + function(data) { + search(query, data); + }); + + } } $(document).ready(function() {