简体   繁体   English

如何使用 Javascript 查找用户定义的 email 地址与 web 列表完全匹配

[英]How to Find the user define email address exact match to a web list using Javascript

the user will provide a specific email address.用户将提供一个特定的 email 地址。 in this case, varEmail = "admin@domain-name.onmicrosoft.com".在本例中,varEmail = "admin@domain-name.onmicrosoft.com"。 now I have to check this on the web page list programmatically.现在我必须以编程方式在 web 页面列表上检查它。 I should be able to return the exact match of the user defined email address on the web page list.我应该能够返回 web 页面列表中用户定义的 email 地址的精确匹配。

Currently, I have these (2) email addresses on my trial environment.目前,我的试用环境中有这 (2) 个 email 地址。 在此处输入图像描述

I am also using this Javascript to get one of the email in the web page list.我还使用这个 Javascript 来获取 web 页面列表中的 email 之一。

(Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(name => name.textContent.match(/admin@domain-name.onmicrosoft.com/)))

how ever I am getting 2 output instead of the exact user define email address.我怎么得到 2 output 而不是确切的用户定义 email 地址。 Output: psi-admi@domain-name.onmicrosoft.com admin@domain-name.onmicrosoft.com Output: psi-admi@domain-name.onmicrosoft.com admin@domain-name.onmicrosoft.com

在此处输入图像描述

I am hoping that there is someone here will noticed my post and help me on this.我希望这里有人会注意到我的帖子并帮助我解决这个问题。 thank you all in advanced!谢谢大家!


Just an update, Here's what I've tried so far.只是一个更新,这是我到目前为止所尝试过的。

Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(acct => acct.textContent === 'admin@domain-name.onmicrosoft.com');

Return Empty Empty Result返回空空结果

By the way, below is the screenshot of the page from where I need to get the Email/Account.顺便说一句,下面是我需要从中获取电子邮件/帐户的页面的屏幕截图。 Compliance New Content Search合规新内容搜索

I'm guessing the data in your HTML has some garbage in there, that's why exact match won't find what you need.我猜你的 HTML 中的数据有一些垃圾,这就是为什么精确匹配找不到你需要的东西。

Try to clean the string up before comparing it, something like:在比较之前尝试清理字符串,例如:

filter(name => name.textContent.trim().toLowerCase() === 'admin@domain-name.onmicrosoft.com')

that way you remove all the white spaces at the beginning and the end of the string, and convert it to lowercase (just in case someone messed up)这样你就可以删除字符串开头和结尾的所有空格,并将其转换为小写(以防万一有人搞砸了)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/tolowercase https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/tolowercase

This inquiry has been resolved.此查询已解决。 Thanks for the help of my Manager, who figured this all out.感谢我的经理的帮助,他解决了这一切。 Again, I've just witnessed a cool pro dev's work ethic, from Identifying the where and how, the right search keywords, and so on.再一次,我刚刚见证了一个很酷的专业开发人员的职业道德,从识别位置和方式、正确的搜索关键字等等。

So, yay, New feed, new learning!所以,耶,新提要,新学习!

here's the line of code he shared with me.这是他与我分享的代码行。

const userRow = document.evaluate("//div[text()='admin@domain-name.onmicrosoft.com']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.offsetParent;

He checked and Analyzed the result from the console.他检查并分析了控制台的结果。 Since I am using 'textContent' or 'innerText', We went there to see its value.由于我使用的是 'textContent' 或 'innerText',我们去那里看看它的价值。 We found that both had an excess string attached to them ( eg usernameadmin@domain-name.onmicrosoft.com ) , that's why the above-suggested solutions didn't work (By the way, I would like to thank @Facundo & @tromgy).我们发现两者都附加了多余的字符串(例如 usernameadmin@domain-name.onmicrosoft.com ) ,这就是上述建议的解决方案不起作用的原因(顺便说一下,我要感谢@Facundo 和@tromgy ).

From there, He finds ways to work on it and come up with a solution.从那里,他找到了解决问题的方法并提出了解决方案。

Please see link below for your reference:请参阅以下链接供您参考:

How to get element by innerText 如何通过innerText获取元素

HTMLElement.offsetParent HTMLElement.offsetParent

Introduction to using XPath in JavaScript JavaScript中XPath的使用介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM