1
0

GS.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. setup.qsp_gs_statistics ??= {};
  2. setup.qsp_gs = function(output,...args){
  3. const startTime = (new Date()).getTime();
  4. let destination = '';
  5. let destinationArgs = [];
  6. if(typeof args[0] == 'string'){
  7. destination = args[0];
  8. destinationArgs = args.slice(1);
  9. }else if(Array.isArray(args[0])){
  10. destination = args[0][0];
  11. destinationArgs = args[0].slice(1);
  12. }else{
  13. console.error(`Argument not reckognized in setup.gs()`,args[0]);
  14. }
  15. if(setup.Overwrite.gs(destination.toLowerCase(),destinationArgs) !== undefined)
  16. return;
  17. State.variables.ARGSstack.push(destinationArgs);
  18. if(setup.userStyle?.logGS)
  19. console.log("GS",args,destination,destinationArgs,clone(State.variables.ARGSstack));
  20. setup.qsp_callStack.push({type:"gs",passage:destination,args:destinationArgs});
  21. var code = `<<include '${destination.toLowerCase()}'>>`;
  22. try{
  23. if(output)
  24. $(output).wiki(code);
  25. else if($(".passage").length)
  26. $(".passage").wiki(code);
  27. else{
  28. $.wiki(code);
  29. }
  30. }
  31. catch(e){
  32. console.error(e);
  33. }
  34. finally{
  35. State.variables.ARGSstack.pop();
  36. setup.qsp_callStack.pop();
  37. }
  38. setup.updateActions();
  39. const endTime = (new Date()).getTime();
  40. const executionTime = endTime - startTime;
  41. if(setup.userStyle?.logGS){
  42. setup.qsp_gs_statistics[destination.toLowerCase()] ??= [];
  43. setup.qsp_gs_statistics[destination.toLowerCase()].push({executionTime: executionTime,args:destinationArgs,callStack: clone(setup.qsp_callStack)});
  44. }
  45. }
  46. Macro.add('gs', {
  47. skipArgs : false,
  48. handler : function () {
  49. try{
  50. setup.qsp_gs(this.output,...this.args[0]);
  51. }
  52. catch (ex) {
  53. return this.error('ERROR in gs-widget: ' + ex.message);
  54. }
  55. }
  56. });