实现一个简单的Gmail contextual gadget
关于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中查找满足条件的链接
折叠复制代码
- <?xmlversion="1.0"encoding="UTF-8"?>
- <ApplicationManifestxmlns="http://schemas.google.com/ApplicationManifest/2009">
- <!-- Support info to show in the marketplace & control panel -->
- <Support>
- <!-- URL for application setup as an optional redirect during the install
- <Linkrel="setup"href="http://www.tech126.com/gadgets/setup.php?domain=${DOMAIN_NAME}"/>
- -->
- <!-- URL for application configuration, accessed from the app settings page in the control panel
- <Linkrel="manage"href="http://www.tech126.com/gadgets/admin.php?domain=${DOMAIN_NAME}"/>
- -->
- <!-- URL explaining how customers get support. -->
- <Linkrel="support"href="http://u.sohu.com/help/help1_free.jsp"/>
- <!-- URL that is displayed to admins during the deletion process, to specify policies such as data retention, how to claim accounts, etc. -->
- <!--<Link rel="deletion-policy" href="http://www.tech126.com/google/deletion-policy.php" />-->
- </Support>
- <!-- Name and description pulled from message bundles -->
- <Name>Sohu Mail UFile</Name>
- <Description>A sohu mail ufile info for Gmail contextual gadgets</Description>
- <!-- Show this link in Google's universal navigation for all users -->
- <Extensionid="navLink"type="link">
- <Name>Sohu Mail UFile</Name>
- <Url>http://u.sohu.com?from=google&domain=${DOMAIN_NAME}</Url>
- </Extension>
- <!-- Declare our OpenID realm so our app is white listed -->
- <Extensionid="realm"type="openIdRealm">
- <Url>http://u.sohu.com</Url>
- </Extension>
- <!-- EXTRACTOR -->
- ;
- <Extensionid="UFileIdExtractor"type="contextExtractor">
- <Name>UFileId</Name>
- <Url>google.com:HttpLinkExtractor</Url>
- <Paramname="url"value=".*u.sohu.com/download/(d+)/(d+)$"/>
- <Triggersref="UFileIdGadget"/>
- <Scoperef="emailSubject"/>
- <Scoperef="emailBody"/>
- <Containername="mail"/>
- </Extension>
- <!-- GADGET -->
- <Extensionid="UFileIdGadget"type="gadget">
- <Name>Sohu Mail UFile contextual gadget</Name>
- <Url>http://www.tech126.com/gadgets/sohu_mail_ufile_gadget.xml</Url>
- <Containername="mail"/>
- </Extension>
- <!-- SCOPE -->
- <Scopeid="emailSubject">
- <Url>tag:google.com,2010:auth/contextual/extractor/SUBJECT</Url>
- <Reason>This application searches the Subject: line of each email for the link "http://u.sohu.com/download".</Reason>
- </Scope>
- <Scopeid="emailBody">
- <Url>tag:google.com,2010:auth/contextual/extractor/BODY</Url>
- <Reason>This application searches the message body of each email for the link "http://u.sohu.com/download".</Reason>
- </Scope>
- </ApplicationManifest>
然后,再调用指定服务器上的gadget.xml去执行相应的操作
折叠复制代码
- <?xmlversion="1.0"encoding="UTF-8"?>
- <Module>
- <ModuleP refstitle="Sohu Mail UFile Link"
- description="The File information"
- height="20"
- author="yushunzhi"
- author_email="yushunzhi@sohu.com"
- author_location="Beijing, China">
- <!-- Declare feature dependencies.
- The first one is not specific to Gmail contextual gadgets. -->
- <Requirefeature="dynamic-height"/>
- <!-- The next feature, google.contentmatch, is required for all
- Gmail contextual gadgets.
- <Param>- specify one or more comma-separated extractor IDs in
- a param named "extractors". This line is overridden by the extractor ID
- in the manifest, but is still expected to be present. -->
- <Requirefeature="google.contentmatch">
- <Paramname="extractors">
- google.com:HttpLinkExtractor
- </Param>
- </Require>
- </ModulePrefs>
- <!-- Define the content type and display location. The settings
- "html" and "card" are required for all Gmail contextual gadgets. -->
- <Contenttype="html"view="card">
- <![CDATA[
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
- <style>
- html, body {
- color:#333333;
- font:12px Arial,Helvetica,sans-serif;
- margin:0;
- padding:0;
- </style>
- </head>
- <script src="http://www.tech126.com/gadgets/jquery-1.4.2.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- (function ()
- {
- var exp = /u.sohu.com/download/(d+)/(d+)/;
- var matches = google.contentmatch.getContentMatches();
- var numLoaded = 0;
- var exp_match = [];
- var url = '', bid = '', fileid = '';
- var img_postfix = ['jpg','jpeg','gif','png'];
- var matchList = document.createElement('ul');
- var listItem, childItem;
- document.body.appendChild(matchList);
- for(var i = 0 ; i < matches.length ; ++i)
- {
- url = matches.url;
- exp_match = url.match(exp);
- if (!exp_match)
- {
- continue;
- }
- bid = exp_match[1];
- fileid = exp_match[2];
- $.ajax(
- {
- url: "http://u.sohu.com/uFileInfo.action",
- dataType: 'jsonp',
- data: {'fileId':fileid},
- jsonp: 'callBack',
- success: function(json)
- {
- json = json || {};
- listItem = document.createElement('li');
- listItem.innerHTML = "File Url: " + "http://u.sohu.com/download/" + json.fileSe rver + "/" + json.fileId;
- childItem = document.createElement('div');
- childItem.innerHTML = "File Name: " + json.fileName;
- listItem.appendChild(childItem);
- childItem = document.createElement('div');
- childItem.innerHTML = "File Size: " + json.fileSize;
- listItem.appendChild(childItem);
- childItem = document.createElement('div');
- childItem.innerHTML = "File User: " + json.fileUser;
- listItem.appendChild(childItem);
- childItem = document.createElement('div');
- childItem.innerHTML = "File Type: " + json.fileType;
- listItem.appendChild(childItem);
- if (img_postfix.indexOf(json.fileType.toLowerCase()) >= 0)
- {
- childItem = document.createElement('img');
- childItem.src = "http://u.sohu.com/file/preview/image/" + json.fileServer + "/" + json.fileId;
- listItem.appendChild(childItem);
- }
- childItem = document.createElement('p');
- listItem.appendChild(childItem);
- matchList.appendChild(listItem);
- },
- timeout: 3000
- }
- );
- }
- gadgets.window.adjustHeight(200);
- })();
- </script>
- ]]><I></I>
- </Content>
- </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后的截图:
我前几天也做了一个contextual gadgets,不知道你有没有发现,如果邮件主体有中文,gadget针对邮件主体的extractor就不会被触发?Subject也有类似的情况。