PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/calendar/birthday_manager/birthday_manager_fb.user.js

http://gdata-javascript-client.googlecode.com/
JavaScript | 156 lines | 82 code | 23 blank | 51 comment | 11 complexity | d6faaa93cbef2c28af7d00b5c795e46f MD5 | raw file
  1. /* FacebookBirthdayManager
  2. * Copyright (c) 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * This script relies on facebook URL scheme.
  17. * If it breaks, it can likely be fixed by changing what's passed into the various xpath commands.
  18. * This works in conjunction with the Birthday Manager, a sample for the Javascript API.
  19. */
  20. // ==UserScript==
  21. // @name Facebook Birthday Manager
  22. // @namespace http://www.google.com
  23. // @description Adds a link on profile pages that will let you add a user's birthday to your calendar.
  24. // @include *.thefacebook.com/profile.php*
  25. // @include *.facebook.com/profile.php*
  26. // ==/UserScript==
  27. // Facebook shows month names. This maps them to 1-12.
  28. var monthNumMappings = {
  29. 'January': 1,
  30. 'February': 2,
  31. 'March': 3,
  32. 'April': 4,
  33. 'May': 5,
  34. 'June': 6,
  35. 'July': 7,
  36. 'August': 8,
  37. 'September': 9,
  38. 'October': 10,
  39. 'November': 11,
  40. 'December': 12
  41. };
  42. /**
  43. * This utility function uses a Firefox-specific function
  44. * to perform an XPath search on the document nodes.
  45. * @param {String} Xpath query
  46. * @return {XPathResult} Nodes found from query.
  47. */
  48. function xPath(query) {
  49. return document.evaluate(
  50. query,
  51. document,
  52. null,
  53. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  54. null);
  55. }
  56. /**
  57. * This function looks for the birthday, name, and picture on the page.
  58. * It returns if it cannot find any of the required areas.
  59. * If successful, it calls the function to create the button link.
  60. */
  61. function insertAddLink() {
  62. var monthDayLinks = xPath('//a[contains(@href, "s.php?adv&k=10010")]');
  63. if (monthDayLinks.snapshotLength > 0) { //take the first
  64. var monthDayLink = monthDayLinks.snapshotItem(0);
  65. var monthDayText = monthDayLink.innerHTML;
  66. var monthText = monthDayText.split(' ')[0];
  67. var dayText = monthDayText.split(' ')[1];
  68. } else {
  69. return;
  70. }
  71. var yearLinks = xPath('//a[contains(@href, "b.php?k=10010&n=-1&y1=")]');
  72. if (yearLinks.snapshotLength > 0) { //take the first
  73. var yearLink = yearLinks.snapshotItem(0);
  74. var yearText = yearLink.innerHTML;
  75. } else {
  76. return;
  77. }
  78. var picImgs = xPath('//a[contains(@href, "album.php?profile")]/img')
  79. if (picImgs.snapshotLength > 0) { //take the first
  80. var picImg = picImgs.snapshotItem(0);
  81. } else {
  82. var picImg = '';
  83. }
  84. var nameHs = xPath('//div[@class="profile_name"]/h2');
  85. if (nameHs.snapshotLength > 0) { //take the first
  86. var nameH = nameHs.snapshotItem(0);
  87. var nameText = nameH.lastChild.nodeValue;
  88. } else {
  89. var nameText = 'Unknown name';
  90. }
  91. var profileUrl = window.location.href;
  92. // Create button link
  93. var addLink = makeAgeNode(nameText, dayText, monthText,
  94. yearText, picImg.src, profileUrl);
  95. // Append new button link after year div
  96. if (yearLink.nextSibling == null) {
  97. yearLink.parentNode.appendChild(addLink);
  98. } else {
  99. yearLink.parentNode.insertBefore(addLink, node.nextSibling);
  100. }
  101. }
  102. /**
  103. * This function inserts an image that links to the Birthday Manager,
  104. * with the user data in the query string.
  105. * @param {String} nameText
  106. * @param {String} dayText
  107. * @param {String} monthText
  108. * @param {String} yearText
  109. * @param {String} picImgSrc
  110. * @param {String} profileUrl
  111. * @return {Node} Span containing image link
  112. */
  113. function makeAgeNode(nameText, dayText, monthText, yearText, picImgSrc, profileUrl) {
  114. var container = document.createElement('span');
  115. var addToCalImage = document.createElement('img');
  116. addToCalImage.setAttribute('src',
  117. 'http://gdata-javascript-client.googlecode.com/' +
  118. 'svn/trunk/samples/calendar/birthday_manager/' +
  119. '/images/birthdayreminder_addtocal.png');
  120. var link = document.createElement('a');
  121. var baseUrl = 'http://gdata-javascript-client.googlecode.com/' +
  122. 'svn/trunk/samples/calendar/birthday_manager/' +
  123. 'birthday_manager.html';
  124. var url = baseUrl + '?name=' + escape(nameText) + '&birthdate=' +
  125. monthNumMappings[monthText] + '/' + dayText + '/' + yearText +
  126. '&image=' + picImgSrc + '&profile=' + profileUrl;
  127. link.setAttribute('href', url);
  128. link.appendChild(addToCalImage);
  129. container.appendChild(document.createTextNode(' '));
  130. container.appendChild(link);
  131. return (container);
  132. }
  133. insertAddLink();