User:Docmoates/Social: Difference between revisions

From XMethod Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 280: Line 280:
<script>
<script>
function initSocialFeed(){
function initSocialFeed(){
var apiBase=location.protocol+'//'+location.host+'/api.php';
var apiBase=mw.util.wikiScript('api');
var user='Guest';
var user=mw.config.get('wgUserName')||'Guest';
var csrfToken='';
var api=new mw.Api();
var mwApi=null;
 
try{
if(window.mw){
user=mw.config.get('wgUserName');
if(!user)user='Guest';
if(mw.Api)mwApi=new mw.Api();
}
}catch(e){}


var photo=localStorage.getItem('sf_photo_'+user)||'';
var photo=localStorage.getItem('sf_photo_'+user)||'';
Line 316: Line 307:


function apiGet(params){
function apiGet(params){
if(mwApi){
return api.get(params);
return mwApi.get(params);
}
var url=apiBase+'?format=json';
for(var k in params)url+='&'+encodeURIComponent(k)+'='+encodeURIComponent(params[k]);
return fetch(url,{credentials:'include'}).then(function(r){return r.json();});
}
}


function apiPost(params){
function apiPost(params){
if(mwApi){
return api.postWithToken('csrf',params);
return mwApi.postWithToken('csrf',params);
}
var form=new FormData();
form.append('format','json');
for(var k in params)form.append(k,params[k]);
return fetch(apiBase,{method:'POST',credentials:'include',body:form}).then(function(r){return r.json();});
}
}


function getToken(){
function getToken(){
if(mwApi){
return api.getToken('csrf');
return mwApi.getToken('csrf').then(function(token){
csrfToken=token;
return token;
});
}
return apiGet({action:'query',meta:'tokens',type:'csrf'}).then(function(d){
csrfToken=d.query.tokens.csrftoken;
return csrfToken;
});
}
}


function uploadFile(file,prefix,onProgress,onDone,onError){
function uploadFile(file,prefix,onProgress,onDone,onError){
getToken().then(function(token){
var filename=prefix+'_'+user+'_'+Date.now()+'.'+file.name.split('.').pop();
var filename=prefix+'_'+user+'_'+Date.now()+'.'+file.name.split('.').pop();
var formData=new FormData();
api.upload(file,{filename:filename,ignorewarnings:true}).progress(function(pct){
formData.append('action','upload');
onProgress(pct*100);
formData.append('filename',filename);
}).done(function(resp){
formData.append('file',file);
formData.append('token',token);
formData.append('format','json');
formData.append('ignorewarnings','1');
 
var xhr=new XMLHttpRequest();
xhr.open('POST',apiBase,true);
xhr.withCredentials=true;
xhr.upload.onprogress=function(e){if(e.lengthComputable)onProgress(Math.round((e.loaded/e.total)*100));};
xhr.onload=function(){
if(xhr.status===200){
try{
var resp=JSON.parse(xhr.responseText);
if(resp.upload){
if(resp.upload){
if(resp.upload.imageinfo){onDone(resp.upload.imageinfo.url);}
if(resp.upload.imageinfo){onDone(resp.upload.imageinfo.url);}
else if(resp.upload.warnings){onError('Warning: '+JSON.stringify(resp.upload.warnings));}
else{onError('Upload issue: '+JSON.stringify(resp.upload));}
else{onError('Upload issue');}
}else{onError('Upload failed');}
}else if(resp.error){onError(resp.error.info);}
}).fail(function(code,result){
else{onError('Unknown error');}
onError('Upload error: '+(result.error?result.error.info:code));
}catch(e){onError('Parse error');}
});
}else{onError('Upload failed: '+xhr.status);}
};
xhr.onerror=function(){onError('Network error');};
xhr.ontimeout=function(){onError('Timeout');};
xhr.timeout=60000;
xhr.send(formData);
}).catch(function(e){onError('Token error: '+e);});
}
}


Line 982: Line 932:
}
}


if(window.mw){
mw.loader.using(['mediawiki.api','mediawiki.util']).then(initSocialFeed);
mw.loader.using(['mediawiki.api']).then(initSocialFeed).catch(function(){initSocialFeed();});
}else{
document.addEventListener('DOMContentLoaded',initSocialFeed);
}
</script>
</script>
</div>
</div>
</html>
</html>

Revision as of 03:17, 3 February 2026

Add Story
?
Edit
📍
📝
No posts yet
Be the first to share something!
Set Profile Photo
Upload
From URL
📸
Click to select or drag photo here

Uploading...

Add Story
Upload
From URL
📷
Click to select or drag image here

Uploading...

Add Photo
Upload
From URL
📤
Click to select or drag image here

Uploading...

Add Video
Upload
From URL
🎬
Click to select or drag video here

Uploading...

Supports YouTube, Vimeo, or direct video URLs

Add Location

— or —