关于Gmail contextual gadget是什么,可以参考这里

 

简单的说,Gmail contextual gadget是一个能在Gmail里触发的gadget

 

邮件的Subject,Body,From,To...等包含某个指定的文本或者link时,均可以触发改gadget

 

实现一个Gmail contextual gadget还算比较简单,我们只需要实现Manifest和Gadget.xml即可

 

以下是一个简单的Manifest文件,使用了google.com:HttpLinkExtractor,在Subject和Body中查找满足条件的链接

 


折叠复制代码




































































  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <ApplicationManifestxmlns="http://schemas.google.com/ApplicationManifest/2009">
  3.  
  4. <!-- Support info to show in the marketplace & control panel -->
  5. <Support>
  6.  
  7. <!-- URL for application setup as an optional redirect during the install
  8. <Linkrel="setup"href="http://www.tech126.com/gadgets/setup.php?domain=${DOMAIN_NAME}"/>
  9. -->
  10.  
  11. <!-- URL for application configuration, accessed from the app settings page in the control panel
  12. <Linkrel="manage"href="http://www.tech126.com/gadgets/admin.php?domain=${DOMAIN_NAME}"/>
  13. -->
  14.  
  15. <!-- URL explaining how customers get support. -->
  16. <Linkrel="support"href="http://u.sohu.com/help/help1_free.jsp"/>
  17.  
  18. <!-- URL that is displayed to admins during the deletion process, to specify policies such as data retention, how to claim accounts, etc. -->
  19. <!--<Link rel="deletion-policy" href="http://www.tech126.com/google/deletion-policy.php" />-->
  20. </Support>
  21.  
  22. <!-- Name and description pulled from message bundles -->
  23. <Name>Sohu Mail UFile</Name>
  24. <Description>A sohu mail ufile info for Gmail contextual gadgets</Description>
  25.  
  26. <!-- Show this link in Google's universal navigation for all users -->
  27. <Extensionid="navLink"type="link">
  28. <Name>Sohu Mail UFile</Name>
  29. <Url>http://u.sohu.com?from=google&domain=${DOMAIN_NAME}</Url>
  30. </Extension>
  31.  
  32. <!-- Declare our OpenID realm so our app is white listed -->
  33. <Extensionid="realm"type="openIdRealm">
  34. <Url>http://u.sohu.com</Url>
  35. </Extension>
  36.  
  37. <!-- EXTRACTOR -->
  38.   ;
  39. <Extensionid="UFileIdExtractor"type="contextExtractor">
  40. <Name>UFileId</Name>
  41. <Url>google.com:HttpLinkExtractor</Url>
  42. <Paramname="url"value=".*u.sohu.com/download/(d+)/(d+)$"/>
  43. <Triggersref="UFileIdGadget"/>
  44. <Scoperef="emailSubject"/>
  45. <Scoperef="emailBody"/>
  46. <Containername="mail"/>
  47. </Extension>
  48.  
  49. <!-- GADGET -->
  50.  
  51. <Extensionid="UFileIdGadget"type="gadget">
  52. <Name>Sohu Mail UFile contextual gadget</Name>
  53. <Url>http://www.tech126.com/gadgets/sohu_mail_ufile_gadget.xml</Url>
  54. <Containername="mail"/>
  55. </Extension>
  56.  
  57. <!-- SCOPE -->
  58.  
  59. <Scopeid="emailSubject">
  60. <Url>tag:google.com,2010:auth/contextual/extractor/SUBJECT</Url>
  61. <Reason>This application searches the Subject: line of each email for the link "http://u.sohu.com/download".</Reason>
  62. </Scope>
  63.  
  64. <Scopeid="emailBody">
  65. <Url>tag:google.com,2010:auth/contextual/extractor/BODY</Url>
  66. <Reason>This application searches the message body of each email for the link "http://u.sohu.com/download".</Reason>
  67. </Scope>
  68.  
  69. </ApplicationManifest>


 

然后,再调用指定服务器上的gadget.xml去执行相应的操作

 


折叠复制代码





























































  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <Module>
  3. <ModuleP refstitle="Sohu Mail UFile Link"
  4. description="The File information"
  5. height="20"
  6. author="yushunzhi"
  7. author_email="yushunzhi@sohu.com"
  8. author_location="Beijing, China">
  9.  
  10. <!-- Declare feature dependencies.
  11. The first one is not specific to Gmail contextual gadgets. -->
  12. <Requirefeature="dynamic-height"/>
  13.  
  14. <!-- The next feature, google.contentmatch, is required for all
  15. Gmail contextual gadgets.
  16. <Param>- specify one or more comma-separated extractor IDs in
  17. a param named "extractors". This line is overridden by the extractor ID
  18. in the manifest, but is still expected to be present. -->
  19. <Requirefeature="google.contentmatch">
  20. <Paramname="extractors">
  21. google.com:HttpLinkExtractor
  22. </Param>
  23. </Require>
  24. </ModulePrefs>
  25.  
  26. <!-- Define the content type and display location. The settings
  27. "html" and "card" are required for all Gmail contextual gadgets. -->
  28. <Contenttype="html"view="card">
  29. <![CDATA[
  30. <head>
  31. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  32. <style>
  33. html, body {
  34. color:#333333;
  35. font:12px Arial,Helvetica,sans-serif;
  36. margin:0;
  37. padding:0;
  38. </style>
  39. </head>
  40. <script src="http://www.tech126.com/gadgets/jquery-1.4.2.min.js" type="text/javascript"></script>
  41. <script type="text/javascript">
  42.  
  43. (function ()
  44. {
  45. var exp = /u.sohu.com/download/(d+)/(d+)/;
  46. var matches = google.contentmatch.getContentMatches();
  47. var numLoaded = 0;
  48. var exp_match = [];
  49. var url = '', bid = '', fileid = '';
  50. var img_postfix = ['jpg','jpeg','gif','png'];
  51. var matchList = document.createElement('ul');
  52. var listItem, childItem;
  53.  
  54. document.body.appendChild(matchList);
  55.  
  56. for(var i = 0 ; i < matches.length ; ++i)
  57. {
  58. url = matches.url;
  59. exp_match = url.match(exp);
  60. if (!exp_match)
  61. {
  62. continue;
  63. }
  64.  
  65. bid = exp_match[1];
  66. fileid = exp_match[2];
  67.  
  68. $.ajax(
  69. {
  70. url: "http://u.sohu.com/uFileInfo.action",
  71. dataType: 'jsonp',
  72. data: {'fileId':fileid},
  73. jsonp: 'callBack',
  74. success: function(json)
  75. {
  76. json = json || {};
  77. listItem = document.createElement('li');
  78. listItem.innerHTML = "File Url: " + "http://u.sohu.com/download/" + json.fileSe rver + "/" + json.fileId;
  79. childItem = document.createElement('div');
  80. childItem.innerHTML = "File Name: " + json.fileName;
  81. listItem.appendChild(childItem);
  82. childItem = document.createElement('div');
  83. childItem.innerHTML = "File Size: " + json.fileSize;
  84. listItem.appendChild(childItem);
  85. childItem = document.createElement('div');
  86. childItem.innerHTML = "File User: " + json.fileUser;
  87. listItem.appendChild(childItem);
  88. childItem = document.createElement('div');
  89. childItem.innerHTML = "File Type: " + json.fileType;
  90. listItem.appendChild(childItem);
  91. if (img_postfix.indexOf(json.fileType.toLowerCase()) >= 0)
  92. {
  93. childItem = document.createElement('img');
  94. childItem.src = "http://u.sohu.com/file/preview/image/" + json.fileServer + "/" + json.fileId;
  95. listItem.appendChild(childItem);
  96. }
  97. childItem = document.createElement('p');
  98. listItem.appendChild(childItem);
  99. matchList.appendChild(listItem);
  100. },
  101. timeout: 3000
  102. }
  103. );
  104. }
  105. gadgets.window.adjustHeight(200);
  106.  
  107. })();
  108.  
  109. </script>
  110. ]]><I></I>
  111. </Content>
  112. </Module>




这个Gadgets功能很简单,就是判断如果有Sohu U盘发送的文件链接,则后台使用Jsonp去获取该文件的一些信息


如果文件是图片,那么就生成一个缩略图展示出来


实现后,可以到Google marketplace上去创建一个List,最后不要submit激活,如果激活,Googel会收取$100的费用...


然后,添加到自己的Google Apps domain中去测试,在调试过程中,访问mail最好加上nogadgetcach=1的参数,不让其缓存,如:


https://mail.google.com/a/tech126.com/?nogadgetcache=1#


以下是运行这个Gadget后的截图: