Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Roster iCalendar
- // version 0.5
- // 21-11-2017
- // Copyright (c) 2011, Moraine Didier
- // Released under the GPL license
- // http://www.gnu.org/copyleft/gpl.html
- //
- // --------------------------------------------------------------------
- //
- // This is a Greasemonkey user script. To install it, you need
- // Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
- // Then restart Firefox and revisit this script.
- // Under Tools, there will be a new menu item to "Install User Script".
- // Accept the default configuration and install.
- //
- // Required script:
- // parser.user.js
- //
- // To uninstall, go to Tools/Manage User Scripts,
- // select "Roster iCalendar", and click Uninstall.
- //
- // --------------------------------------------------------------------
- // Changelog
- // V0.5
- // Changes for Greasemonkey 4
- // V0.4
- // Replace button by greasemonkey User Script Command
- // V0.3
- // Adaptation for Greasemonkey
- // V0.2
- // Fix for new CWP design (04/2009)
- // V0.1 Beta
- // Initial release
- //
- // --------------------------------------------------------------------
- // ==UserScript==
- // @name Roster iCalendar
- // @description Script creating an iCalendar file based on the TNT Roster.
- // @include *CWP_WA/CWP_RosterTW.aspx?EMPNO=*
- // @require parser.user.js
- // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
- // @grant GM_registerMenuCommand
- // @grant GM_getValue
- // @grant GM_setValue
- // ==/UserScript==
- // --------------------------------------------------------------------
- // Get objects or variable from the Global object of Greasemonkey
- // --------------------------------------------------------------------
- var addButton = unsafeWindow.addButton;
- var roster = unsafeWindow.roster;
- if (typeof(roster) == 'undefined') {
- alert("Installation Error:\nThe script 'parser.user.js' is required by 'Roster iCalendar'.");
- return;
- }
- // --------------------------------------------------------------------
- // Create "iCal" button
- // --------------------------------------------------------------------
- addButton(
- 'iCalendar',
- 'iCal',
- function(event) {
- try {
- roster.parse();
- roster.turnAroundTime();
- //iCal.checkAlarmConfig();
- iCal.create(roster);
- iCal.popup();
- } catch (e) {
- //unsafeWindow.console.log(e)
- } finally {
- event.preventDefault();
- }
- }
- )
- // --------------------------------------------------------------------
- // VEvent object representing a VEVENT entry in an iCalendar file
- // --------------------------------------------------------------------
- function VEvent(activity) {
- // documentation
- // http://www.kanzaki.com/docs/ical/
- this.dtstart = activity.std.getiCalTime();
- if (activity.checkin != '') {
- this.dtstart = activity.checkin.getiCalTime();
- } else {
- this.dtstart = activity.std.getiCalTime();
- }
- this.dtend = activity.sta.getiCalTime();
- if (typeof activity.checkout === "object")
- this.dtend = activity.checkout.getiCalTime();
- this.summary = this.createSummary(activity);
- this.description = this.createDescription(activity);
- this.location = activity.departure;
- this.categories = (roster.isActivityFlight(activity)) ? "Flight" : "";
- this.uid = this.createUID(activity);
- //this.valarm = new VAlarm(activity);
- //this.attendee= [];
- }
- VEvent.prototype = {
- // [activity.std]-[activity-name]@tay.be
- createUID: function(activity) {
- return activity.std + "-" + activity.name + "@tay.be"
- },
- createDescription: function(activity) {
- if (! roster.isActivityFlight(activity)) return "";
- var desc = "";
- // 00:00 |
- if (activity.checkin != '')
- desc += activity.checkin.getTime() + 'Z | ';
- // LGG 00:00 | CIA 00:00
- desc += activity.departure + ' ' + activity.std.getTime() + 'Z | ';
- desc += activity.arrival + ' ' + activity.sta.getTime() + 'Z';
- // | 00:00
- if (typeof activity.checkout === "object") {
- desc += ' | ' + activity.checkout.getTime() + 'Z\\n';
- } else {
- desc += '\\n';
- }
- // Turn-around: 00:00
- if (activity.turnaround != null)
- desc += 'Turn-around: ' + activity.turnaround + '\\n';
- // Crew: xxxxx(CP)
- desc += 'Crew: ' + activity.crew + '\\n';
- // Note:
- if (activity.note.length > 4)
- desc += 'Note: ' + activity.note + '\\n';
- // Duty design: Z
- if (activity.duty != "")
- desc += 'Duty design: ' + activity.duty + '\\n';
- // Import date
- desc += 'Event imported ' + new Date();
- return desc;
- },
- createSummary: function(activity) {
- if (! roster.isActivityFlight(activity)) return activity.name;
- return activity.name + ': ' + activity.departure + ' ' + activity.arrival
- },
- // January 19, 1998, at 0700 UTC --> 19980119T070000Z
- __dtstamp: function() {
- var now = new Date();
- var dt = now.getUTCFullYear() + '';
- dt += this.__format(now.getUTCMonth() + 1);
- dt += this.__format(now.getUTCDate());
- dt += 'T';
- dt += this.__format(now.getUTCHours());
- dt += this.__format(now.getUTCMinutes());
- dt += this.__format(now.getUTCSeconds());
- dt += 'Z';
- return dt;
- },
- __format: function(num) {
- if (num.toString().length == 1) {
- return "0".concat(num)
- } else {
- return num
- }
- },
- toString: function() {
- var evt = "BEGIN:VEVENT\n";
- evt += "DTSTAMP:" + this.__dtstamp() + '\n';
- evt += "UID:" + this.uid + '\n';
- evt += "DTSTART:" + this.dtstart + '\n';
- evt += "DTEND:" + this.dtend + '\n';
- evt += "SUMMARY:" + this.summary + '\n';
- evt += "DESCRIPTION:" + this.description + '\n';
- evt += "LOCATION:" + this.location + '\n';
- evt += "CATEGORIES:" + this.categories + '\n';
- //evt += this.valarm.toString();
- evt += "END:VEVENT\n";
- return evt
- }
- }
- // --------------------------------------------------------------------
- // VAlarm object representing a VALARM entry in an iCalendar file
- // --------------------------------------------------------------------
- //function VAlarm(activity) {
- // this.isFlight = this.isActivityFlight(activity);
- // this.trigger = this.getTrigger(activity.checkin, activity.departure);
- //}
- //
- //VAlarm.prototype = {
- // getTrigger: function(checkin, dep) {
- // if (checkin == null)
- // return;
- //
- // // ex: rosterscripts.valarm.LGG.checkin.trigger = 60
- // return GM_getValue("valarm." + dep + ".checkin.trigger", null);
- // },
- //
- // Test if an activity is a flight
- // (activity name length > 3)
- // ex: TAY123 vs SBB
- // or starts with 3V
- // isActivityFlight: function(activity) {
- // return (activity.name.match(/3V*/));
- // },
- //
- // toString: function() {
- // if (!GM_getValue("valarm.enable", false) || this.trigger == null || !this.isFlight)
- // return "";
- //
- // var txt = "BEGIN:VALARM\n";
- // txt += "TRIGGER:-PT" + this.trigger + "M\n";
- // txt += "ACTION:AUDIO\n";
- // txt += "END:VALARM\n";
- // return txt;
- // }
- //}
- // --------------------------------------------------------------------
- // iCalendar object
- // --------------------------------------------------------------------
- var iCal = {
- ical: null,
- header: "BEGIN:VCALENDAR\n" +
- "VERSION:2.0\n" +
- "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n" +
- "METHOD:PUBLISH\n",
- footer: "END:VCALENDAR",
- // Create the iCal text
- create: function(roster) {
- this.ical = this.header;
- for (activity of roster.activities) {
- this.ical += new VEvent(activity).toString();
- }
- this.ical += this.footer;
- return this.ical;
- },
- // Open a popup window and allow the user to download the iCal text
- popup: function() {
- var popup = unsafeWindow.open('','','width=500,height=160,dialog,modal');
- dom = popup.document;
- dom.body.innerHTML = '<p style="font-family: Verdana; font-size: 12px;">';
- dom.body.innerHTML += 'Right click on the link and save it on your hard disk:<br>';
- dom.body.innerHTML += '<a href="data:text/calendar;charset=utf-8,' + encodeURI(this.ical) + '">Roster.ics</a>';
- dom.body.innerHTML += '<br/><br/>';
- 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/>';
- dom.body.innerHTML += '<small>Automatic alarms are ';
- dom.body.innerHTML += GM_getValue("valarm.enable", false) ? 'enabled' : 'disabled';
- dom.body.innerHTML += '.</small>';
- dom.body.innerHTML += '</p>';
- //dom.close();
- },
- // Check alarm configuration in about:config
- //checkAlarmConfig: function() {
- // if (!GM_getValue("valarm.enable", false))
- // GM_setValue("valarm.enable", false);
- //},
- }
- // vim: set et ts=4 sw=4:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement