我当前正在跑步:
for (var el of document.querySelectorAll('script[if="inlineJs"]'))
{
var m = el.textContent.match(/"([^"]*webId*[^"]*)"/);
var webid = m;
if (webid) return webid[1];
这将返回:
{"cache":{"Co.context.configCtx":{"webId":"lex-orlando","locale":"en_US","version":"LIVE","page":"HomePage","secureSiteId":"4441a718647510048bfc0003ba0acbe3"},"features":{"directivePerfSwitch":true,"enable.directive.localisation":true,"enable.directive.thumbnailGallery":true,"enable.new.newstaticmap":false,"disable.forms.webId":false,"use.hydra.popup.title.override.via.url":true,"enable.directive.geoloc.enableHighAccuracy":true,"use.hydra.theme.service":true,"disable.ajax.options.contentType":false,"dealerLocator.map.use.markerClustering":true,"hydra.open.login.popup.on.cs.click":false,"hydra.consumerlogin.use.secure.cookie":true,"use.hydra.directive.vertical.thumbnailGallery.onpopup":true,"hydra.encrypt.data.to.login.service":true,"disable.dealerlocator.fix.loading":false,"use.hydra.date.formatting":true,"use.hydra.optimized.style.directive.updates":false,"hydra.click.pmp.button.on.myaccount.page":true,"use.hydra.fix.invalid.combination.of.filters":true,"disable.vsr.view.from.preference":false}},"store":{"properties":{"routePrefix":"/hydra-graph"}}}
我希望它只返回:lex-orlando
当我跑步时:
for (var el of document.querySelectorAll('script[if="inlineJs"]')) {
var m = el.textContent.match("webId");
return m[0];
它仅返回“ webID”
反正有使用textContent.match返回“ lex-orlando”吗?
Bonues:如果webID从前缀“ lex”更改为“ motp”,“ gmps”,“ motp”怎么办?
任何帮助将不胜感激。我正在转动车轮试图解决这个问题。
我也尝试过:
for (var el of document.querySelectorAll('script[if="inlineJs"]')) {
var m = el.textContent.match(/"([^"]*webId*[^"]*)"/);
var obj = JSON.parse(m);
var myVar = obj.cache["Co.context.configCtx"].webId;
window.alert("webId is " + myVar);
return myVar;
但是这将返回一个空值。我尝试更改事件侦听器,但无济于事。这就是为什么我试图查看是否可以通过textContext.match
完成答案 0 :(得分:0)
您不应因JSON.parse()
的结果而致电el.textContent.match()
。首先,它返回一个数组,而不是单个字符串。其次,该正则表达式将仅匹配webId
属性名称,而不是整个对象。
我认为您应该只解析整个textContent
。
var obj = JSON.parse(el.textContent);
var webid = obj["Co.context.configCtx"].webId;