从源代码中获取存储在变量中的元素

时间:2022-07-05 16:19:35

I have this piece of code:

我有这段代码:

chrome.runtime.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {
    message.innerText = request.source;
  }
});

I need to get the text from an element which is in that source code. For example, consider the page having an element like:

我需要从源代码中的元素中获取文本。例如,考虑具有以下元素的页面:

<a class="something">something goes here</a>

I need to get that text of class 'something' with JavaScript. The request.source returns the source code with structure as it is.

我需要用JavaScript获取类'something'的文本。 request.source返回具有结构的源代码。

1 个解决方案

#1


0  

You should be able to turn the source code into jQuery object and use it like usual from there.

您应该能够将源代码转换为jQuery对象,并像往常一样使用它。

var requestedSource = request.source;
$(requestedSource).find('a.something').first().text();

This works for me in a very simple test on jsfiddle.

这对我来说是一个非常简单的jsfiddle测试。

Note that you may have to play around with whatever $.find() returns if you have more than one anchor element with the class "something" (I just used $.first() to simplify my example). In that case, you can iterate through the results of $.find() like an array.

请注意,如果你有多个带有“something”类的锚元素,你可能不得不玩$ .find()返回(我只使用$ .first()来简化我的例子)。在这种情况下,您可以像数组一样遍历$ .find()的结果。

If that solution doesn't work, another (but worse) way you could do it would be to write the requested code to a hidden div, and then run $.find() from the div (though if the first solution doesn't work, there is likely something going wrong with request.source itself, so check its contents).

如果该解决方案不起作用,另一种(但更糟)的方法是将所请求的代码写入隐藏的div,然后从div运行$ .find()(尽管第一种解决方案不是工作,request.source本身可能出现问题,所以检查其内容)。

For example:

$('body').append('<div id="requestedSource" style="display: none;"></div>');

And then:

$("#requestedSource").append(request.source);
$("#requestedSource").find("a.something").first().text();

If you're repeating this request often, you can also empty the hidden div by calling $.empty() on it when you're done processing:

如果你经常重复这个请求,你也可以在完成处理时通过调用$ .empty()清空隐藏的div:

$("#requestedSource").empty();

For best performance, you would want to store everything in a variable and write once:

为了获得最佳性能,您需要将所有内容存储在变量中并写入一次:

var hiddenRequestSource = '<div id="requestedSource" style="display: none;">';
hiddenRequestSource += request.source;
hiddenRequestSource += '</div>';

$('body').append(hiddenRequestSource);  

var myResults = $("#requestedSource").find("a.something").first().text();
$("#requestedSource").empty();

#1


0  

You should be able to turn the source code into jQuery object and use it like usual from there.

您应该能够将源代码转换为jQuery对象,并像往常一样使用它。

var requestedSource = request.source;
$(requestedSource).find('a.something').first().text();

This works for me in a very simple test on jsfiddle.

这对我来说是一个非常简单的jsfiddle测试。

Note that you may have to play around with whatever $.find() returns if you have more than one anchor element with the class "something" (I just used $.first() to simplify my example). In that case, you can iterate through the results of $.find() like an array.

请注意,如果你有多个带有“something”类的锚元素,你可能不得不玩$ .find()返回(我只使用$ .first()来简化我的例子)。在这种情况下,您可以像数组一样遍历$ .find()的结果。

If that solution doesn't work, another (but worse) way you could do it would be to write the requested code to a hidden div, and then run $.find() from the div (though if the first solution doesn't work, there is likely something going wrong with request.source itself, so check its contents).

如果该解决方案不起作用,另一种(但更糟)的方法是将所请求的代码写入隐藏的div,然后从div运行$ .find()(尽管第一种解决方案不是工作,request.source本身可能出现问题,所以检查其内容)。

For example:

$('body').append('<div id="requestedSource" style="display: none;"></div>');

And then:

$("#requestedSource").append(request.source);
$("#requestedSource").find("a.something").first().text();

If you're repeating this request often, you can also empty the hidden div by calling $.empty() on it when you're done processing:

如果你经常重复这个请求,你也可以在完成处理时通过调用$ .empty()清空隐藏的div:

$("#requestedSource").empty();

For best performance, you would want to store everything in a variable and write once:

为了获得最佳性能,您需要将所有内容存储在变量中并写入一次:

var hiddenRequestSource = '<div id="requestedSource" style="display: none;">';
hiddenRequestSource += request.source;
hiddenRequestSource += '</div>';

$('body').append(hiddenRequestSource);  

var myResults = $("#requestedSource").find("a.something").first().text();
$("#requestedSource").empty();