bcc-to-gdocs

Jquery commands to get data from forms, ala datatel

当前为 2014-09-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name bcc-to-gdocs
  3. // @description Jquery commands to get data from forms, ala datatel
  4. // @include https://go.bergen.edu/WebAdvisor/WebAdvisor?TOKENIDX*&APP=ST&CONSTITUENCY=WBFC
  5. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
  6. // @namespace https://greasyfork.org/scripts/4752-bcc-to-gdocs
  7. // @version 0.0.1.20140902214734
  8. // ==/UserScript==
  9.  
  10. /* On WebAdvisor class rosters add buttons for BCC faculty to display student names
  11. ** in easily cut-and-pasted to spreadsheet form, and automatically create the spreadsheet
  12. ** in Google Docs. I am using the ClientLogin functions from http://userscripts.org/scripts/show/27673
  13. ** which was written by Michael Freeman and AJAX functions from
  14. ** http://gdatatips.blogspot.com/2009/07/create-new-google-docs-spreadsheet-from.html
  15. ** by Eric from Google.
  16. ** and loads of JQuery
  17. */
  18.  
  19. /*
  20. ** Professor of Art and Animation
  21. ** Francis Scrubjay Schmidt 2009, updated 2011, updated 2014
  22. ** Bergen Community College
  23. */
  24.  
  25. var gdocsbuttonfront = '<input name="GDOCS" class="gdocsbutton" value=" To GoogleDocs " id=';
  26. var gdocsbuttonback = ' class="Button"> </td>'; //WebAdvisor looking button straight to gdocs
  27. var rollcallbuttonfront = '<input name="GDOCSROLLCALL" class="rollcallbutton" value=" Roll Call " id=';
  28. var rollcallbuttonback = ' class="Button"> </td>'; //WebAdvisor looking button straight to gdocs
  29. var login = '<p> Gmail Login &nbsp &nbsp &nbsp &nbsp <input id="name" size=24 type="text"> </p>'; //Gmail user name
  30. var password = '<p> Gmail Password &nbsp <input id="password" size=24 type="password"> </p>'; //Google password
  31. var cellbuttonfront = '<td> <input name="C&P" class="rostbutton" value=" Roster '; // first half names button
  32. var cellbuttonmiddle = '" id="'; // middle of button
  33. var cellbuttonback = '" class="shortButton">'; // back half names button
  34. var cellblank = '<th class=" FranAdded"> <div> <p id="blank"> <input type="hidden" name="blank" value="o"> </p> </div> </th>';
  35. var classname = []; //where the classname will go
  36. var csv = ''; //where the csv version is
  37. var names = "";
  38. var hrefs = [];//the URLS for AJAX student names
  39. var hoursroom = [];//class meeting times and room
  40. var term = [];//term
  41.  
  42. // Gdocs login stuff at top of page
  43. $('<TextNode textContent="Class Roster Select Section">').each(function () { // Make sure we are on the right page
  44. $('<p> &nbsp </p>').appendTo("#screenTitle span:contains('Class Roster Select Section')"); // make a line between
  45. $(login).appendTo("#screenTitle span:contains('Class Roster Select Section')"); // put username
  46. $(password).appendTo("#screenTitle span:contains('Class Roster Select Section')"); // put password
  47. // Place to attach buttons for each class
  48. $("#GROUP_Grp_LIST_VAR2").each(function () { // find the table with the classnames
  49. var classcount = 0;
  50. $(this).find('table').each(function () { // find the main table
  51. $(this).find('tr:first').each(function () { // find the header
  52. $(this).prepend(cellblank); // add a spacer to match rows
  53. });
  54. $(this).find('tr:gt(0):not(:has(th))').each(function () { // get rows greater than header (0)
  55. classcount ++; //increment the class #
  56. $(this).prepend(gdocsbuttonfront + classcount + gdocsbuttonback + rollcallbuttonfront + classcount + rollcallbuttonback + cellbuttonfront + classcount + cellbuttonmiddle + classcount + cellbuttonback); // put the button in to grab and print student names
  57. hoursroom[classcount] = $(this).find('.LIST_VAR6 > div > p').text();//the hours and room
  58. term[classcount] = $(this).find('.LIST_VAR4 > div > p').text();// get the term
  59. $(this).find('td:visible').each(function () { //get each cell of the row
  60. $(this).find('a').each(function () { // grabs 'a' tags, with URL of student names for course
  61. classname[classcount] = $(this).text(); //for the popup window
  62. hrefs[classcount] = $(this).attr("href"); // gets the actual URL
  63. });
  64. });
  65. }); // end class row reads
  66. }); // end table read
  67. // What the Roster button does
  68. $(".rostbutton").click(function(){ // Action for class roster button to be pushed to make window
  69. var target = $(this).attr("id"); //get the # of the row, assign to 'target'
  70. $.get(hrefs[target], function(data){ //AJAX get url data
  71. var rosternames = $(data).find('#GROUP_Grp_LIST_VAR7').html();//student name class table data
  72. myWindow = window.open('', 'MyNewWindow', 'width=900,height=500,left=100,top=100,scrollbars= 1'); // makes popup
  73. myWindow.document.write('<html><head><title>' + classname[target]+'</title></head><body><font face="geneva, helvetica, arial"><div><table>' + rosternames + '</table></div></font></body></html>'); // page html
  74. myWindow.document.close();
  75. myWindow.focus();
  76. });
  77. });
  78. $(".rollcallbutton").click(function() { // Action for rollcall button when clicked do this
  79. // login modified from http://userscripts.org/scripts/show/27673
  80. // ajax modified from http://gdatatips.blogspot.com/2009/07/create-new-google-docs-spreadsheet-from.html
  81. var gauthURL = 'https://www.google.com/accounts/ClientLogin'; // using clientlogin method from google api
  82. var gToken = ''; // where the string that lets us authenticate lives
  83. var loginInfo = 'accountType=HOSTED_OR_GOOGLE&service=writely&source=bcc-to-gdocs-Francis-Schmidt'; // header stuff for xml
  84. var target = $(this).attr("id");
  85. $.get(hrefs[target], function(data){ //AJAX get url data
  86. $(data).find('#GROUP_Grp_LIST_VAR7').each(function (){
  87. csv = "1,3,," + '\n';
  88. csv += "Name,ID Number," + '\n'; //header row for csv
  89. $(this).find('tr:gt(1)').each(function () {
  90. csv += '"'+$(this).find('td.LIST_VAR7 > div > a ').text()+'"' + ','; //student name for csv
  91. csv += $(this).find('td.LIST_VAR6 > div > p').text() + '\n'; //student # for csv
  92. });
  93. }); //end student name class table data
  94. }); //end AJAX response
  95. //These pieces form the xml that is sent to google docs
  96. var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
  97. '<entry xmlns="http://www.w3.org/2005/Atom">',
  98. '<category scheme="http://schemas.google.com/g/2005#kind"',
  99. ' term="http://schemas.google.com/docs/2007#spreadsheet"/>',
  100. '<title>', "$ROLLCALL " + term[target] + " " + classname[target], '</title>', //name the spreadsheet after the classname
  101. '</entry>'].join('');
  102. var body = ['--END_OF_PART\r\n',
  103. 'Content-Type: application/atom+xml;\r\n\r\n',
  104. atom, '\r\n',
  105. '--END_OF_PART\r\n',
  106. 'Content-Type: text/csv\r\n\r\n',
  107. csv, '\r\n', // the actual data from webadvisor
  108. '--END_OF_PART--\r\n'].join('');
  109. //logininfo has the useremail and password added to the xml needed for authentication
  110. loginInfo = loginInfo + '&Email=' + $("#name").val();
  111. loginInfo = loginInfo + '&Passwd=' + $("#password").val();
  112. GM_xmlhttpRequest({ // greasemonkey xml
  113. method: 'POST',
  114. url: gauthURL,
  115. headers: {
  116. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  117. 'Content-type': 'application/x-www-form-urlencoded'
  118. },
  119. data: loginInfo, // the authentication
  120. onload: function(responseDetails){
  121. if(responseDetails.status != 200) { // failure
  122. alert('Whoa. Did you log in? \n\n Error: ' + responseDetails.status + ': ' + responseDetails.statusText);
  123. }
  124. if (responseDetails.status == 200) { // 200 means we are successful, so we send up the spreadsheet
  125. tokenText = responseDetails.responseText; // get the text
  126. gToken = tokenText.match(/Auth=[a-z0-9_-]+/i); // turn it into our token
  127. GM_xmlhttpRequest({ // let's light this candle
  128. method: 'POST',
  129. url: 'http://docs.google.com/feeds/documents/private/full',
  130. headers: {
  131. 'Authorization': 'GoogleLogin ' + gToken,
  132. 'Content-Type': 'multipart/related; boundary=END_OF_PART',
  133. 'Slug': term[target] + " " + classname[target] +'.csv'
  134. }, // proves we are allowed to, and says what we are going to do
  135. contentType: 'multipart/related; boundary=END_OF_PART',
  136. data: body, //the xml we made above, called body
  137. dataType: 'xml',
  138. onload: function(responseDetails){
  139. if (responseDetails.status == 201) { // success!
  140. alert('Spreadsheet named ' + "$ROLLCALL"+ term[target] + " " + classname[target] + '\n is now in ' + $("#name").val() + ' docs account');
  141. }
  142. if(responseDetails.status != 201) { // failure ;(
  143. alert('Whoops! There was a problem \n\n Error: ' + responseDetails.status + ': ' + responseDetails.statusText);
  144. }
  145. showLoad(false);
  146. }
  147. });//end putting stuff up on google
  148. }
  149. }
  150. });
  151. }); //end rollcall button
  152. $(".gdocsbutton").click(function() { // Action for gdocs button when clicked do this
  153. // login modified from http://userscripts.org/scripts/show/27673
  154. // ajax modified from http://gdatatips.blogspot.com/2009/07/create-new-google-docs-spreadsheet-from.html
  155. var gauthURL = 'https://www.google.com/accounts/ClientLogin'; // using clientlogin method from google api
  156. var gToken = ''; // where the string that lets us authenticate lives
  157. var loginInfo = 'accountType=HOSTED_OR_GOOGLE&service=writely&source=bcc-to-gdocs-Francis-Schmidt'; // header stuff for xml
  158. var target = $(this).attr("id");
  159. $.get(hrefs[target], function(data){ //AJAX get url data
  160. $(data).find('#GROUP_Grp_LIST_VAR7').each(function (){
  161. csv = '"' + hoursroom[target] + '"' + '\n';
  162. csv += "Name,ID Number,Email Address,Phone Number" + '\n'; //header row for csv
  163. $(this).find('tr:gt(1)').each(function () {
  164. csv += '"'+$(this).find('td.LIST_VAR7 > div > a ').text()+'"' + ','; //student # for csv
  165. csv += $(this).find('td.LIST_VAR6 > div > p').text() + ','; //student # for csv
  166. csv += $(this).find('td.LIST_VAR8 > div > a ').text() + ','; //student # for csv
  167. csv += $(this).find('td.VAR_LIST1 > div > p').text() + '\n'; //student # for csv
  168. });
  169. }); //end student name class table data
  170. }); //end AJAX response
  171. //These pieces form the xml that is sent to google docs
  172. var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
  173. '<entry xmlns="http://www.w3.org/2005/Atom">',
  174. '<category scheme="http://schemas.google.com/g/2005#kind"',
  175. ' term="http://schemas.google.com/docs/2007#spreadsheet"/>',
  176. '<title>', term[target] + " " + classname[target], '</title>', //name the spreadsheet after the classname
  177. '</entry>'].join('');
  178. var body = ['--END_OF_PART\r\n',
  179. 'Content-Type: application/atom+xml;\r\n\r\n',
  180. atom, '\r\n',
  181. '--END_OF_PART\r\n',
  182. 'Content-Type: text/csv\r\n\r\n',
  183. csv, '\r\n', // the actual data from webadvisor
  184. '--END_OF_PART--\r\n'].join('');
  185. //logininfo has the useremail and password added to the xml needed for authentication
  186. loginInfo = loginInfo + '&Email=' + $("#name").val();
  187. loginInfo = loginInfo + '&Passwd=' + $("#password").val();
  188. GM_xmlhttpRequest({ // greasemonkey xml
  189. method: 'POST',
  190. url: gauthURL,
  191. headers: {
  192. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  193. 'Content-type': 'application/x-www-form-urlencoded'
  194. },
  195. data: loginInfo, // the authentication
  196. onload: function(responseDetails){
  197. if(responseDetails.status != 200) { // failure
  198. alert('Whoa. Did you log in? \n\n Error: ' + responseDetails.status + ': ' + responseDetails.statusText);
  199. }
  200. if (responseDetails.status == 200) { // 200 means we are successful, so we send up the spreadsheet
  201. tokenText = responseDetails.responseText; // get the text
  202. gToken = tokenText.match(/Auth=[a-z0-9_-]+/i); // turn it into our token
  203. GM_xmlhttpRequest({ // let's light this candle
  204. method: 'POST',
  205. url: 'http://docs.google.com/feeds/documents/private/full',
  206. headers: {
  207. 'Authorization': 'GoogleLogin ' + gToken,
  208. 'Content-Type': 'multipart/related; boundary=END_OF_PART',
  209. 'Slug': term[target] + " " + classname[target] +'.csv'
  210. }, // proves we are allowed to, and says what we are going to do
  211. contentType: 'multipart/related; boundary=END_OF_PART',
  212. data: body, //the xml we made above, called body
  213. dataType: 'xml',
  214. onload: function(responseDetails){
  215. if (responseDetails.status == 201) { // success!
  216. alert('Spreadsheet named ' + term[target] + " " + classname[target] + '\n is now in ' + $("#name").val() + ' docs account');
  217. }
  218. if(responseDetails.status != 201) { // failure ;(
  219. alert('Whoops! There was a problem \n\n Error: ' + responseDetails.status + ': ' + responseDetails.statusText);
  220. }
  221. showLoad(false);
  222. }
  223. });//end putting stuff up on google
  224. }
  225. }
  226. });
  227. }); //end google docs button
  228. }); // end main table read
  229. });