====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
script_external_editor_script_importer [2016/10/18 10:51] cdfa |
script_external_editor_script_importer [2017/07/06 13:15] (current) cdfa 1.3 |
||
---|---|---|---|
Line 2: | Line 2: | ||
* Purpose: automatically import .js files elsewhere on your device into lightning launcher when they have changed. This allows you to use external script editors! | * Purpose: automatically import .js files elsewhere on your device into lightning launcher when they have changed. This allows you to use external script editors! | ||
* Author: cdfa | * Author: cdfa | ||
- | * Version: 1.2 | + | * Version: 1.3 |
* Link: https://plus.google.com/100430440392224314007/posts/KAoKQ317trG | * Link: https://plus.google.com/100430440392224314007/posts/KAoKQ317trG | ||
+ | |||
======Changelog===== | ======Changelog===== | ||
+ | 1.3 | ||
+ | * Add support for subfolders | ||
+ | |||
+ | 1.2 | ||
* fixed importing of large scripts | * fixed importing of large scripts | ||
* updated to new api | * updated to new api | ||
Line 10: | Line 15: | ||
====== How to use the script ====== | ====== How to use the script ====== | ||
Recommended to run the script on resumed event of the container which is your test environment, but you can use it really anytime anywhere. | Recommended to run the script on resumed event of the container which is your test environment, but you can use it really anytime anywhere. | ||
+ | |||
====== Script code ====== | ====== Script code ====== | ||
<sxh javascript> | <sxh javascript> | ||
Line 34: | Line 40: | ||
return ""; | return ""; | ||
} | } | ||
+ | } | ||
+ | |||
+ | function updateScripts(folder){ | ||
+ | folder.listFiles().forEach(function(file){ | ||
+ | var fileName = file.getName() | ||
+ | var length = fileName.length | ||
+ | if(file.isDirectory()){ | ||
+ | updateScripts(file); | ||
+ | }else if(fileName.substring(length-3,length)==".js"){ | ||
+ | var name = fileName.slice(0,-3); | ||
+ | var path = folder.getPath().substring(scriptFolder.length); | ||
+ | var script=getScriptByPathAndName(path, name); | ||
+ | if(script==null){ | ||
+ | Toast.makeText(context, "creating new script: "+name, Toast.LENGTH_SHORT).show(); | ||
+ | script = createScript(path, name,"",0); | ||
+ | var tagdate=0; | ||
+ | }else{ | ||
+ | var tagdate = script.getTag("lmdate"); | ||
+ | } | ||
+ | var lmdate = file.lastModified() | ||
+ | if(lmdate>tagdate||typeof tagdate == "undefined"){ | ||
+ | updatedScripts.push(name); | ||
+ | script.setText(read(folder.getPath()+"/"+fileName)); | ||
+ | script.setTag("lmdate",time) | ||
+ | } | ||
+ | } | ||
+ | }); | ||
} | } | ||
Line 40: | Line 73: | ||
var updatedScripts = []; | var updatedScripts = []; | ||
var folder = new File(scriptFolder) | var folder = new File(scriptFolder) | ||
- | var files = folder.listFiles() | + | updateScripts(folder) |
- | if(!files)return; | + | |
- | for(i=0;i<files.length;i++){ | + | |
- | var fileName = files[i].getName() | + | |
- | var length = fileName.length | + | |
- | if(fileName.substring(length-3,length)==".js"){ | + | |
- | var name = fileName.slice(0,-3); | + | |
- | var script=getScriptByName(name); | + | |
- | if(script==null){ | + | |
- | Toast.makeText(context, "creating new script: "+name, Toast.LENGTH_SHORT).show(); | + | |
- | script = createScript(name,"",0,importPath); | + | |
- | var tagdate=0; | + | |
- | }else{ | + | |
- | var tagdate = script.getTag("lmdate"); | + | |
- | } | + | |
- | var lmdate = files[i].lastModified() | + | |
- | if(lmdate>tagdate||typeof tagdate == "undefined"){ | + | |
- | updatedScripts.push(name); | + | |
- | script.setText(read(scriptFolder+"/"+fileName)); | + | |
- | script.setTag("lmdate",time) | + | |
- | } | + | |
- | } | + | |
- | } | + | |
if( updatedScripts.length>0)Toast.makeText(context, "Updated scripts: "+JSON.stringify( updatedScripts), Toast.LENGTH_SHORT).show(); | if( updatedScripts.length>0)Toast.makeText(context, "Updated scripts: "+JSON.stringify( updatedScripts), Toast.LENGTH_SHORT).show(); | ||
</sxh> | </sxh> |