-
-
Save Utopiah/a93046ed01bea7f2a61651ceadc12ae1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see also for Hubs specific utils | |
// https://gist.github.com/Utopiah/1cfc123239fa2994569fc7c5c60b2928 | |
console.log(`Usage: | |
pimvrSaveRemote('PIMVRdata', 'testUtils', JSON.stringify({test:'ok'}) ) | |
pimvrReadRemote('PIMVRdata', 'testUtils', console.log)`) | |
const baseURL = 'https://iterative-explorations.com/sandbox/cms/index.php/' | |
// const baseURL = 'https://fabien.benetou.fr/Tools/' | |
function pimvrSaveRemote(group = 'PIMVRdata', page = 'TestUtils', data = JSON.stringify({test: 'ok'}) ) { | |
// allow to avoid fetch CORS issues | |
const writeURL = baseURL+group+'/'+page+'?action=edit'; | |
var myWriteRequest = new XMLHttpRequest(); | |
myWriteRequest.open('POST', writeURL, true); | |
myWriteRequest.setRequestHeader("Content-Type", | |
"application/x-www-form-urlencoded"); | |
myWriteRequest.onreadystatechange = function () { | |
if (myWriteRequest.readyState === 4) { | |
//console.log(myWriteRequest.responseText); | |
console.log("Save on "+page+" sucessful"); | |
} | |
}; | |
console.log("trying to open "+writeURL+"post=1&author=PIMVR&authpw=edit_password&text="+data) | |
myWriteRequest.send("post=1&author=PIMVR&authpw=edit_password&text="+data); | |
// cf http://www.pmwiki.org/wiki/PmWiki/EditingAPI | |
// removed to go live, use instead WebSockets or a JSON store e.g. https://jsonbox.io | |
} | |
/* | |
//cleaner and more compact version | |
var writeURL = 'https://fabien.benetou.fr/PIMVRdata/URLBucketTest?action=edit'; | |
fetch(writeURL, { | |
method: 'POST', | |
headers: {'Content-Type':'application/x-www-form-urlencoded'}, | |
body: "post=1&author=PIMVR&authpw=edit_password&text="+window.location | |
}).then(res => res).then(res => console.log(res)) | |
*/ | |
function pimvrReadRemote(group = 'PIMVRdata', page = 'TestUtils', callback = console.log){ | |
fetch(baseURL+group+'/'+page+'?action=source') | |
.then( response => { return response.json() } ) | |
.then( data => callback(data) ) | |
} | |
function pimvrReadRawRemote(group = 'PIMVRdata', page = 'TestUtils', callback = console.log){ | |
fetch(baseURL+group+'/'+page+'?action=source') | |
.then( response => { return response.text() } ) | |
.then( data => callback(data) ) | |
} | |
//pimvrReadRawRemote('Tools', 'RecentChanges', processPageList) | |
function processPageList(rawPmWiki){ | |
var pages = [] | |
for (var line of rawPmWiki.split('\n')){ | |
var matches = line.match(/\[\[(.*)\/(.*)\]\] /) | |
if (matches && matches.length) { | |
var [_, _, pageName] = matches | |
pages.push(baseURL + pageName) | |
} | |
} | |
return pages | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment