Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从电子邮件中过滤出YouTube视频的链接_Javascript_Google Apps Script - Fatal编程技术网

Javascript 从电子邮件中过滤出YouTube视频的链接

Javascript 从电子邮件中过滤出YouTube视频的链接,javascript,google-apps-script,Javascript,Google Apps Script,我正在使用谷歌脚本获取电子邮件的正文。我试图让它只保存包含在普通YouTube电子邮件中的链接,该电子邮件会通知您来自新用户的上传。我想,使用JavaScript只保存单击电子邮件中包含的图像时所跟随的链接。下面是电子邮件部分的外观示例: 电子邮件的内容如下: <div> <table width="620" cellspacing="0" cellpadding="0" border="0" align="center"><tr><td bg

我正在使用谷歌脚本获取电子邮件的正文。我试图让它只保存包含在普通YouTube电子邮件中的链接,该电子邮件会通知您来自新用户的上传。我想,使用JavaScript只保存单击电子邮件中包含的图像时所跟随的链接。下面是电子邮件部分的外观示例:

电子邮件的内容如下:

    <div>
<table width="620" cellspacing="0" cellpadding="0" border="0" align="center"><tr><td bgcolor="#F0F0F0">
<table width="578" cellspacing="0" cellpadding="0" border="0" align="center">
<tr>
<td height="16"></td>
</tr>
<tr>
<td align="center" valign="top">
<img src="http://s.ytimg.com/yt/img/email/digest/email_header.png">
</td>
</tr>
<tr>
<td height="16"></td>
</tr>
<tr>
<td align="left" bgcolor="#FFFFFF">
<div style="border-style:solid;border-width:1px;border-color:#cccccc">
<table width="578" cellspacing="0" cellpadding="0" border="0" align="center">
<tr>
<td height="22" colspan="3"></td>
</tr>
<tr>
<td width="40"></td>
<td width="498">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td bgcolor="#FFFFFF" align="left" width="50">
<img src="https://i.ytimg.com/i/N5jZlcpSP00e_zShDw9EfQ/1.jpg" height="50" width="50">
</td>
<td width="16"></td>
<td>
<div style="font-family:arial,Arial,sans-serif;font-size:18px;color:#333333;line-height:24px" height="59">
FatalDubstepHD has uploaded <b>Empressto - No Love [Dubstep]</b>.
</div>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" border="0">
<tr><td height="22"></td></tr>
</table>
<div style="border-bottom:1px solid #e1e1e1"></div>
<table cellspacing="0" cellpadding="0" border="0">
<tr><td height="22"></td></tr>
</table>
<table cellspacing="0" cellpadding="0" border="0" style="width:480px;margin:0 9px">
<tr>
<td>
<a href="http://www.youtube.com/watch?v=zTYri0ei45Q&amp;feature=em-uploademail" style="text-decoration:none;display:block" class="nonplayable" target="_blank">
<img src="http://i2.ytimg.com/vi/zTYri0ei45Q/hqdefault.jpg" height="360" width="480" border="0">
</a>
</td>
</tr>
<tr><td height="16"></td>
</tr>
<tr>
<td width="480">
<a href="http://www.youtube.com/watch?v=zTYri0ei45Q&amp;feature=em-uploademail" style="text-decoration:none" target="_blank">
<div style="font-family:arial,Arial,sans-serif;font-size:15px;color:#333333;line-height:20px;font-weight:bold">
Empressto - No Love [Dubstep]
</div>
</a>
<div style="font-family:arial,Arial,sans-serif;font-size:13px;color:#999999;line-height:20px">
by
<a href="http://www.youtube.com/user/FatalDubstepHD?feature=em-uploademail" style="font-size:13px;color:#999999;line-height:20px;text-decoration:none" target="_blank">FatalDubstepHD</a>
</div>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" border="0">
<tr><td height="16"></td></tr>
</table>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="498">
<div style="font-family:arial,Arial,sans-serif;font-size:13px;color:#333333;line-height:20px">
● Free Download<br><a href="https://soundcloud.com/empresstoro/empressto-no-love" target="_blank">https://soundcloud.com/<WBR>empresstoro/empressto-no-love</a><br><br>● Empressto <br><a href="http://www.facebook.com/EmpreSSto" target="_blank">http://www.facebook.com/<WBR>EmpreSSto</a><br><a href="http://www.twitter.com/Empressto" target="_blank">http://www.twitter.com/<WBR>Empressto</a><br><a href="http://www.youtube.com/user/TheOfficialEmpressto" target="_blank">http://www.youtube.com/user/<WBR>TheOfficialEmpressto</a><br><br>● Fatal Dubstep<br><a href="http://www.facebook.com/FatalMusicUK" target="_blank">http://www.facebook.com/<WBR>FatalMusicUK</a><br><a href="http://www.twitter.com/FatalMusicUK" target="_blank">http://www.twitter.com/<WBR>FatalMusicUK</a><br><br>● Owner Of Fatal<br><a href="http://www.twitter.com/Mihalis_Costa" target="_blank">http://www.twitter.com/<WBR>Mihalis_Costa</a><br><br>● Contact Email/Submit Music<br><a href="mailto:fatalmusicsubmission@gmail.com" target="_blank">fatalmusicsubmission@gmail.com</a>
</div>
</td>
</tr>
</table>
</td>
<td width="40"></td>
</tr>
<tr>
<td height="22" colspan="3"></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td height="16"></td>
</tr>
<tr>
<td align="center">
<table cellspacing="0" cellpadding="0" border="0" align="center">
<tr>
<td width="40"></td>
<td width="498">
<div style="color:#999999;line-height:14px;font-size:11px;font-family:arial,Arial,sans-serif">
You were sent this email because you chose to receive updates about new videos uploaded by <a href="http://www.youtube.com/user/FatalDubstepHD?feature=em-uploademail" style="text-decoration:none;color:#1c62b9" target="_blank">FatalDubstepHD</a>. If you don&#39;t want these updates anymore, you can change your preferences by visiting the <a href="http://www.youtube.com/subscription_manager" style="text-decoration:none;color:#1c62b9" target="_blank">subscription manager</a>.
</div>
</td>
<td width="40"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="16"></td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" align="center">
<tr>
<td width="40"></td>
<td width="498">
<div style="color:#999999;font-size:11px;line-height:14px;font-family:arial,Arial,sans-serif">
©2013 YouTube, LLC 901 Cherry Ave, San Bruno, CA 94066
</div>
</td>
<td width="40"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="22"></td>
</tr>
</table>
</td></tr></table>
</div>
编辑2:新代码:

function processInbox() {
  // get all threads in inbox
  var label = GmailApp.getUserLabelByName("Social/YouTube/Dubstep");
  var doneLabel = GmailApp.getUserLabelByName("Social/YouTube/Dubstep (added)");
  var messages = label.getThreads();
  var max = Math.min(messages.length,5);
  for( var i = 0; i < max; ++i ) {
    var email = messages[i].getMessages()[0];
    var contents = email.getBody();
    var st = contents.indexOf('<a href="http://www.youtube.com/watch?v=');
    var str1 = contents.substr(st+9,80);
    var end = str1.indexOf('&amp');
    var link = str1.substring(0,end);
    Logger.log(link); 
    GmailApp.sendEmail('EMAIL', 'Xml Contents', Logger.getLog());

    Logger.clear();

    //messages[i].markRead();
    //messages[i].removeLabel(label).addLabel(doneLabel);
  }
函数processInbox(){
//获取收件箱中的所有线程
var label=GmailApp.getUserLabelByName(“Social/YouTube/Dubstep”);
var doneLabel=GmailApp.getUserLabelByName(“Social/YouTube/Dubstep(added)”;
var messages=label.getThreads();
var max=Math.min(messages.length,5);
对于(变量i=0;ivar st=contents.indexOf(“我想最好的方法是使用
XML服务来解析HTML并从中获取元素

请参阅,将lenient参数设置为
true

不久前我用过类似的方法,你可以看到这个想法


编辑: 我没有用这种方法成功。。。 我使用字符串操作尝试了其他操作:

...
var contents = email.getBody();
var st = contents.indexOf('<a href="http://www.youtube.com/watch?v=');
var str1 = contents.substr(st+9,80);
var end = str1.indexOf('&amp');
var link = str1.substring(0,end);
Logger.log(link);    
。。。
var contents=email.getBody();

var st=contents.indexOf('这个方法并没有真正使用Google脚本中的函数,是吗?我已经用getBody()更新了一封电子邮件的确切输出。是否真的可以使用谷歌脚本的内置功能获取XML数据???XML服务是谷歌脚本中的一项defauld服务…我真的不理解你的评论。如果我遗漏了什么,很抱歉。我添加了我当前正在使用的代码。是否有任何方法可以像现在这样使用代码,或者我必须完全更改它以使用你的XML方法?如果我必须改变它,我会怎么做?我对Javascript和Google脚本非常陌生,所以我一直在寻找一个基本的解决方案,但我想它不会是一个解决方案?它不是很难玩…我这边没有类似的电子邮件要测试,但是
XML。parse
将返回
元素
你可以
log
查看它是如何构建的,然后获取您要查找的
href
。另一篇文章中的示例演示了如何实现。我在主文章中更新了代码,显示了我尝试过的代码。我通过电子邮件发送的内容与以前的内容部分完全相同。它的格式根本不像xml。我做错了什么吗?
...
var contents = email.getBody();
var st = contents.indexOf('<a href="http://www.youtube.com/watch?v=');
var str1 = contents.substr(st+9,80);
var end = str1.indexOf('&amp');
var link = str1.substring(0,end);
Logger.log(link);