Facebook Event Exporter

Export Facebook events

当前为 2015-12-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Facebook Event Exporter
  3. // @namespace http://boris.joff3.com
  4. // @version 1.2
  5. // @description Export Facebook events
  6. // @author Boris Joffe
  7. // @match https://www.facebook.com/events/*
  8. // @grant none
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11. /* eslint-disable no-console, no-unused-vars */
  12. 'use strict';
  13.  
  14. /*
  15. The MIT License (MIT)
  16.  
  17. Copyright (c) 2015 Boris Joffe
  18.  
  19. Permission is hereby granted, free of charge, to any person obtaining a copy
  20. of this software and associated documentation files (the "Software"), to deal
  21. in the Software without restriction, including without limitation the rights
  22. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. copies of the Software, and to permit persons to whom the Software is
  24. furnished to do so, subject to the following conditions:
  25.  
  26. The above copyright notice and this permission notice shall be included in
  27. all copies or substantial portions of the Software.
  28.  
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. THE SOFTWARE.
  36. */
  37.  
  38.  
  39. // Util
  40. var
  41. qs = document.querySelector.bind(document),
  42. qsa = document.querySelectorAll.bind(document),
  43. err = console.error.bind(console),
  44. log = console.log.bind(console),
  45. euc = encodeURIComponent;
  46.  
  47. var DEBUG = false;
  48. function dbg() {
  49. if (DEBUG)
  50. console.log.apply(console, arguments);
  51.  
  52. return arguments[0];
  53. }
  54.  
  55. function qsv(elmStr, parent) {
  56. var elm = parent ? parent.querySelector(elmStr) : qs(elmStr);
  57. if (!elm) err('(qs) Could not get element -', elmStr);
  58. return elm;
  59. }
  60.  
  61. function qsav(elmStr, parent) {
  62. var elm = parent ? parent.querySelectorAll(elmStr) : qsa(elmStr);
  63. if (!elm) err('(qsa) Could not get element -', elmStr);
  64. return elm;
  65. }
  66.  
  67. function setProp(parent, path, val) {
  68. if (!parent || typeof parent !== 'object')
  69. return;
  70. path = Array.isArray(path) ? Array.from(path) : path.split('.');
  71. var child, prop;
  72. while (path.length > 1) {
  73. prop = path.shift();
  74. child = parent[prop];
  75. if (!child || typeof child !== 'object')
  76. parent[prop] = {};
  77. parent = parent[prop];
  78. }
  79. parent[path.shift()] = val;
  80. }
  81.  
  82. function addExportLink() {
  83. log('Event Exporter running');
  84.  
  85. // Event Summary
  86. var evElm = qsv('#event_summary');
  87.  
  88. // Date & Time
  89. // TODO: convert to local time instead of UTC
  90. function convertDateString(dateObj) {
  91. return dateObj.toISOString()
  92. .replace(/-/g, '')
  93. .replace(/:/g, '')
  94. .replace('.000Z', '');
  95. }
  96.  
  97. var sdElm = qsv('[itemprop="startDate"]', evElm);
  98. var sdd = new Date(sdElm.getAttribute('content')),
  99. edd = new Date(sdd);
  100. edd.setHours(edd.getHours() + 1); // Add one hour as a default
  101. var evStartDate = convertDateString(sdd);
  102. var evEndDate = convertDateString(edd);
  103.  
  104. // Location
  105. var locElm = qsv('[data-hovercard]', evElm);
  106. var addrElm = locElm.nextSibling;
  107.  
  108. // Description
  109. var descElm = qs('#event_description').querySelector('span'),
  110. desc;
  111.  
  112. // use innerText for proper formatting, innerText will ship in Firefox 45
  113. if (descElm.innerText) {
  114. // Show full event text so that innerText sees it
  115. setProp(qs('.text_exposed_show', descElm), 'style.display', 'inline');
  116. setProp(qs('.text_exposed_link', descElm), 'style.display', 'none');
  117. desc = descElm.innerText;
  118. } else {
  119. // fallback, HTML encoded entities will appear broken
  120. desc = descElm.innerHTML
  121. .replace(/<br>\s*/g, '\n') // fix newlines
  122. .replace(/<a href="([^"]*)"[^>]*>/g, '[$1] ') // show link urls
  123. .replace(/<[^>]*>/g, ''); // strip html tags
  124. }
  125.  
  126. var ev = {
  127. title : document.title,
  128. startDate : evStartDate,
  129. endDate : evEndDate,
  130. location : locElm.textContent,
  131. address : addrElm.textContent,
  132. description : location.href + '\n\n' + desc
  133. };
  134.  
  135. ev.locationAndAddress = ev.location + ', ' + ev.address;
  136.  
  137. for (var prop in ev) if (ev.hasOwnProperty(prop))
  138. ev[prop] = euc(dbg(ev[prop], ' - ' + prop));
  139.  
  140. // Create link, use UTC timezone to be compatible with toISOString()
  141. var exportUrl = 'https://calendar.google.com/calendar/render?action=TEMPLATE&text=[TITLE]&dates=[STARTDATE]/[ENDDATE]&details=[DETAILS]&location=[LOCATION]&ctz=UTC';
  142.  
  143. exportUrl = exportUrl
  144. .replace('[TITLE]', ev.title)
  145. .replace('[STARTDATE]', ev.startDate)
  146. .replace('[ENDDATE]', ev.endDate)
  147. .replace('[LOCATION]', ev.locationAndAddress)
  148. .replace('[DETAILS]', ev.description);
  149.  
  150. dbg(exportUrl, ' - Export URL');
  151.  
  152. var
  153. evBarElm = qsv('#event_button_bar'),
  154. exportElmLink = qsv('a', evBarElm),
  155. exportElmParent = exportElmLink.parentNode;
  156.  
  157. exportElmLink = exportElmLink.cloneNode();
  158. exportElmLink.href = exportUrl;
  159. exportElmLink.textContent = 'Export Event';
  160.  
  161. // Disable Facebook event listeners (that are attached due to cloning element)
  162. exportElmLink.removeAttribute('ajaxify');
  163. exportElmLink.removeAttribute('rel');
  164.  
  165. // Open in new tab
  166. exportElmLink.setAttribute('target', '_blank');
  167.  
  168. exportElmParent.appendChild(exportElmLink);
  169.  
  170. var evBarLinks = document.querySelectorAll('#event_button_bar a');
  171. Array.from(evBarLinks).forEach(function (a) {
  172. // fix styles
  173. a.style.display = 'inline-block';
  174. });
  175. }
  176.  
  177. function addExportLinkWhenLoaded() {
  178. if (!qs('#event_button_bar') || !qs('#event_description') || !qs('[itemprop="startDate"]')) {
  179. // not loaded
  180. log('page not loaded...');
  181. setTimeout(addExportLinkWhenLoaded, 1000);
  182. } else {
  183. // loaded
  184. log('page loaded...adding link');
  185. addExportLink();
  186. }
  187. }
  188.  
  189. window.addEventListener('load', addExportLinkWhenLoaded, true);