简体   繁体   English

如何使用jQuery定位页面内的字符串/电子邮件地址

[英]How to target strings/email addresses within a page using jQuery

I'm trying to make a script that will search through an entire webpage for email addresses that end in @xyz.com. 我正在尝试创建一个脚本,该脚本将在整个网页中搜索以@ xyz.com结尾的电子邮件地址。 For example: 例如:

$(document).ready(function() {
    $("body:contains('@xyz.com')")
       .css("text-decoration","underline");
  /*$("*:contains('@xyz.com')")
       .css("text-decoration","underline"); doesn't work either */
    I figured contents(); wouldn't be a better choice over contains(); ... 
});

For some reason, it can't seem to detect any email addresses I've hard coded into my page within the paragraphs on the page. 由于某种原因,它似乎无法检测到我在页面上的段落中硬编码到页面中的任何电子邮件地址。

I can't tie the selector down to a specific div since this script will be running on different webpages thus not being able to controll what element the email address is presented in. But... even if I managed to figure that problem out, I still have another problem to deal with... 我无法将选择器绑定到特定的div因为此脚本将在不同的网页上运行,因此无法控制电子邮件地址的显示内容。但是...即使我设法弄清了这个问题,我还有另一个问题要解决...

The email addresses on the webpages will be all random but will all end in @xyz.com. 网页上的电子邮件地址都是随机的,但都以@ xyz.com结尾。 So I'd have to also select anything to left of the @ symbol up until it detects a space between characters. 因此,我还必须选择@符号左侧的任何内容,直到它检测到字符之间的空格。

I can only find solutions to how to detect an email from a list or validation on forms etc. 我只能找到有关如何从列表中检测电子邮件或对表单进行验证的解决方案。

How can I achieve this? 我该如何实现?

The problem is that your script is running in the background page, not in the page. 问题是您的脚本在后台页面而不是在页面中运行。 The background page can't access directly to DOM of pages loaded by chrome. 后台页面无法直接访问由chrome加载的页面的DOM。

To do what you want, look for Content Scripts. 要执行您想要的操作,请查找内容脚本。 This allow you to inject scripts into a given page (like the page loaded in the active tab of chrome). 这样,您就可以将脚本注入到给定的页面中(例如chrome的活动标签中加载的页面)。 This script can perform action on DOM and comunicate with the background page via the Messaging API of chrome. 该脚本可以在DOM上执行操作,并通过chrome的Messaging API与后台页面进行通讯。

Here more informations 这里更多信息

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

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