Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. // Roster iCalendar
  2. // version 0.5
  3. // 21-11-2017
  4. // Copyright (c) 2011, Moraine Didier
  5. // Released under the GPL license
  6. // http://www.gnu.org/copyleft/gpl.html
  7. //
  8. // --------------------------------------------------------------------
  9. //
  10. // This is a Greasemonkey user script. To install it, you need
  11. // Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
  12. // Then restart Firefox and revisit this script.
  13. // Under Tools, there will be a new menu item to "Install User Script".
  14. // Accept the default configuration and install.
  15. //
  16. // Required script:
  17. // parser.user.js
  18. //
  19. // To uninstall, go to Tools/Manage User Scripts,
  20. // select "Roster iCalendar", and click Uninstall.
  21. //
  22. // --------------------------------------------------------------------
  23. // Changelog
  24. // V0.5
  25. // Changes for Greasemonkey 4
  26. // V0.4
  27. // Replace button by greasemonkey User Script Command
  28. // V0.3
  29. // Adaptation for Greasemonkey
  30. // V0.2
  31. // Fix for new CWP design (04/2009)
  32. // V0.1 Beta
  33. // Initial release
  34. //
  35. // --------------------------------------------------------------------
  36. // ==UserScript==
  37. // @name Roster iCalendar
  38. // @description Script creating an iCalendar file based on the TNT Roster.
  39. // @include *CWP_WA/CWP_RosterTW.aspx?EMPNO=*
  40. // @require parser.user.js
  41. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  42. // @grant GM_registerMenuCommand
  43. // @grant GM_getValue
  44. // @grant GM_setValue
  45. // ==/UserScript==
  46.  
  47. // --------------------------------------------------------------------
  48. // Get objects or variable from the Global object of Greasemonkey
  49. // --------------------------------------------------------------------
  50. var addButton = unsafeWindow.addButton;
  51. var roster = unsafeWindow.roster;
  52.  
  53. if (typeof(roster) == 'undefined') {
  54. alert("Installation Error:\nThe script 'parser.user.js' is required by 'Roster iCalendar'.");
  55. return;
  56. }
  57.  
  58. // --------------------------------------------------------------------
  59. // Create "iCal" button
  60. // --------------------------------------------------------------------
  61. addButton(
  62. 'iCalendar',
  63. 'iCal',
  64. function(event) {
  65. try {
  66. roster.parse();
  67. roster.turnAroundTime();
  68. //iCal.checkAlarmConfig();
  69. iCal.create(roster);
  70. iCal.popup();
  71. } catch (e) {
  72. //unsafeWindow.console.log(e)
  73. } finally {
  74. event.preventDefault();
  75. }
  76. }
  77. )
  78.  
  79.  
  80. // --------------------------------------------------------------------
  81. // VEvent object representing a VEVENT entry in an iCalendar file
  82. // --------------------------------------------------------------------
  83. function VEvent(activity) {
  84. // documentation
  85. // http://www.kanzaki.com/docs/ical/
  86. this.dtstart = activity.std.getiCalTime();
  87. if (activity.checkin != '') {
  88. this.dtstart = activity.checkin.getiCalTime();
  89. } else {
  90. this.dtstart = activity.std.getiCalTime();
  91. }
  92.  
  93. this.dtend = activity.sta.getiCalTime();
  94. if (typeof activity.checkout === "object")
  95. this.dtend = activity.checkout.getiCalTime();
  96.  
  97. this.summary = this.createSummary(activity);
  98. this.description = this.createDescription(activity);
  99. this.location = activity.departure;
  100. this.categories = (roster.isActivityFlight(activity)) ? "Flight" : "";
  101. this.uid = this.createUID(activity);
  102. //this.valarm = new VAlarm(activity);
  103. //this.attendee= [];
  104. }
  105.  
  106. VEvent.prototype = {
  107.  
  108. // [activity.std]-[activity-name]@tay.be
  109. createUID: function(activity) {
  110. return activity.std + "-" + activity.name + "@tay.be"
  111. },
  112.  
  113. createDescription: function(activity) {
  114. if (! roster.isActivityFlight(activity)) return "";
  115.  
  116. var desc = "";
  117.  
  118. // 00:00 |
  119. if (activity.checkin != '')
  120. desc += activity.checkin.getTime() + 'Z | ';
  121.  
  122. // LGG 00:00 | CIA 00:00
  123. desc += activity.departure + ' ' + activity.std.getTime() + 'Z | ';
  124. desc += activity.arrival + ' ' + activity.sta.getTime() + 'Z';
  125.  
  126. // | 00:00
  127. if (typeof activity.checkout === "object") {
  128. desc += ' | ' + activity.checkout.getTime() + 'Z\\n';
  129. } else {
  130. desc += '\\n';
  131. }
  132.  
  133. // Turn-around: 00:00
  134. if (activity.turnaround != null)
  135. desc += 'Turn-around: ' + activity.turnaround + '\\n';
  136.  
  137. // Crew: xxxxx(CP)
  138. desc += 'Crew: ' + activity.crew + '\\n';
  139.  
  140. // Note:
  141. if (activity.note.length > 4)
  142. desc += 'Note: ' + activity.note + '\\n';
  143.  
  144. // Duty design: Z
  145. if (activity.duty != "")
  146. desc += 'Duty design: ' + activity.duty + '\\n';
  147.  
  148. // Import date
  149. desc += 'Event imported ' + new Date();
  150.  
  151. return desc;
  152. },
  153.  
  154. createSummary: function(activity) {
  155. if (! roster.isActivityFlight(activity)) return activity.name;
  156. return activity.name + ': ' + activity.departure + ' ' + activity.arrival
  157. },
  158.  
  159. // January 19, 1998, at 0700 UTC --> 19980119T070000Z
  160. __dtstamp: function() {
  161. var now = new Date();
  162. var dt = now.getUTCFullYear() + '';
  163. dt += this.__format(now.getUTCMonth() + 1);
  164. dt += this.__format(now.getUTCDate());
  165. dt += 'T';
  166. dt += this.__format(now.getUTCHours());
  167. dt += this.__format(now.getUTCMinutes());
  168. dt += this.__format(now.getUTCSeconds());
  169. dt += 'Z';
  170.  
  171. return dt;
  172. },
  173.  
  174. __format: function(num) {
  175. if (num.toString().length == 1) {
  176. return "0".concat(num)
  177. } else {
  178. return num
  179. }
  180. },
  181.  
  182. toString: function() {
  183. var evt = "BEGIN:VEVENT\n";
  184. evt += "DTSTAMP:" + this.__dtstamp() + '\n';
  185. evt += "UID:" + this.uid + '\n';
  186. evt += "DTSTART:" + this.dtstart + '\n';
  187. evt += "DTEND:" + this.dtend + '\n';
  188. evt += "SUMMARY:" + this.summary + '\n';
  189. evt += "DESCRIPTION:" + this.description + '\n';
  190. evt += "LOCATION:" + this.location + '\n';
  191. evt += "CATEGORIES:" + this.categories + '\n';
  192. //evt += this.valarm.toString();
  193. evt += "END:VEVENT\n";
  194. return evt
  195. }
  196. }
  197.  
  198. // --------------------------------------------------------------------
  199. // VAlarm object representing a VALARM entry in an iCalendar file
  200. // --------------------------------------------------------------------
  201. //function VAlarm(activity) {
  202. // this.isFlight = this.isActivityFlight(activity);
  203. // this.trigger = this.getTrigger(activity.checkin, activity.departure);
  204. //}
  205. //
  206. //VAlarm.prototype = {
  207. // getTrigger: function(checkin, dep) {
  208. // if (checkin == null)
  209. // return;
  210. //
  211. // // ex: rosterscripts.valarm.LGG.checkin.trigger = 60
  212. // return GM_getValue("valarm." + dep + ".checkin.trigger", null);
  213. // },
  214. //
  215. // Test if an activity is a flight
  216. // (activity name length > 3)
  217. // ex: TAY123 vs SBB
  218. // or starts with 3V
  219. // isActivityFlight: function(activity) {
  220. // return (activity.name.match(/3V*/));
  221. // },
  222. //
  223. // toString: function() {
  224. // if (!GM_getValue("valarm.enable", false) || this.trigger == null || !this.isFlight)
  225. // return "";
  226. //
  227. // var txt = "BEGIN:VALARM\n";
  228. // txt += "TRIGGER:-PT" + this.trigger + "M\n";
  229. // txt += "ACTION:AUDIO\n";
  230. // txt += "END:VALARM\n";
  231. // return txt;
  232. // }
  233. //}
  234.  
  235. // --------------------------------------------------------------------
  236. // iCalendar object
  237. // --------------------------------------------------------------------
  238. var iCal = {
  239.  
  240. ical: null,
  241.  
  242. header: "BEGIN:VCALENDAR\n" +
  243. "VERSION:2.0\n" +
  244. "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n" +
  245. "METHOD:PUBLISH\n",
  246.  
  247. footer: "END:VCALENDAR",
  248.  
  249. // Create the iCal text
  250. create: function(roster) {
  251. this.ical = this.header;
  252. for (activity of roster.activities) {
  253. this.ical += new VEvent(activity).toString();
  254. }
  255. this.ical += this.footer;
  256. return this.ical;
  257. },
  258.  
  259. // Open a popup window and allow the user to download the iCal text
  260. popup: function() {
  261. var popup = unsafeWindow.open('','','width=500,height=160,dialog,modal');
  262. dom = popup.document;
  263. dom.body.innerHTML = '<p style="font-family: Verdana; font-size: 12px;">';
  264. dom.body.innerHTML += 'Right click on the link and save it on your hard disk:<br>';
  265. dom.body.innerHTML += '<a href="data:text/calendar;charset=utf-8,' + encodeURI(this.ical) + '">Roster.ics</a>';
  266. dom.body.innerHTML += '<br/><br/>';
  267. dom.body.innerHTML += 'You can now import this <a href="http://en.wikipedia.org/wiki/ICalendar">iCalendar</a> file into your favorite calendar application (Outlook, Evolution, Lotus Note, Google Calendar, ...)<br/><br/>';
  268. dom.body.innerHTML += '<small>Automatic alarms are ';
  269. dom.body.innerHTML += GM_getValue("valarm.enable", false) ? 'enabled' : 'disabled';
  270. dom.body.innerHTML += '.</small>';
  271. dom.body.innerHTML += '</p>';
  272. //dom.close();
  273. },
  274.  
  275. // Check alarm configuration in about:config
  276. //checkAlarmConfig: function() {
  277. // if (!GM_getValue("valarm.enable", false))
  278. // GM_setValue("valarm.enable", false);
  279. //},
  280. }
  281. // vim: set et ts=4 sw=4:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement