如何正确使用 parseInt 到 select/check 来自 chrome 扩展中的 span 元素

How to properly use parseInt to select/check a span element from within a chrome extension

我正在尝试使用 parseInt 来确定我的 Google Chrome 扩展中的 span 元素是否等于或大于 1。

I am attempting to check for this "0" to change and on change to launch a specific URL - See Image for Reference

Here is a screenshot of the "inspect" option from within chrome.

这是页面 HTML:

<div id="onlinebar" style="text-align: right">

 <a href="javascript:win1()" class="tb_options" title="Kalbėkis su savo piratais.">Užsuk į smuklę</a>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_gang" class="tb_options" onClick="ShowTbPopup(this); obShowHide(0)">My Fleet: (<b>2</b>)</span>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_friend" class="tb_options" onClick="ShowTbPopup(this); obShowHide(1)">My Friends: (<b>2</b>)</span>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_enemies" class="tb_options" onClick="ShowTbPopup(this); obShowHide(2)">My Enemies: (<b>0</b>)</span>&nbsp;&nbsp;&nbsp;&nbsp;

 <a id="closebox" href="#" onclick="javascript:var conf = confirm('Are you sure you want to remove the login box? (It can be turned back on from settings)'); if(conf){ document.getElementById('onlinebar').style.display='none'; setBarPrefs('11769'); }" style="margin-right:20px;"><img src="/images/close.gif" border="0"/></a>

</div>

这是我试过的:

function checkEnemies(){
 numEnemies = parseInt($$("[id^=tb_enemies]")[0].title.split(" ")[0]);
 if (numEnemies >= 1){
  $$("a[href*='sea']")[0].click();
  return true;
 }
 return false;
}


var bool = checkEnemies();

还有这个:

function checkEnemies(){
 numEnemies = parseInt($$('#tb_enemies')[0].title.split(" ")[0]);
 if (numMessages >= 1){
  $$("a[href*='sea']")[0].click();
  return true;
 }
 return false;
}


var bool = checkEnemies();

感谢您提供的任何帮助。

-谢恩

这个数字好像没有设置为title属性的值,你可以直接从元素的内容中获取,这里有一个例子:

// jQuery

let number = parseInt($("#tb_enemies b").text());
console.log("jQuery the number is:", number);

// vanilla JavaScript

number = parseInt(document.querySelector("#tb_enemies b").textContent);
console.log("vanilla JavaScript the number is:", number);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="onlinebar" style="text-align: right">

 <a href="javascript:win1()" class="tb_options" title="Kalbėkis su savo piratais.">Užsuk į smuklę</a>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_gang" class="tb_options" onClick="ShowTbPopup(this); obShowHide(0)">My Fleet: (<b>2</b>)</span>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_friend" class="tb_options" onClick="ShowTbPopup(this); obShowHide(1)">My Friends: (<b>2</b>)</span>&nbsp;&nbsp;| &nbsp;&nbsp;

 <span id="tb_enemies" class="tb_options" onClick="ShowTbPopup(this); obShowHide(2)">My Enemies: (<b>0</b>)</span>&nbsp;&nbsp;&nbsp;&nbsp;

 <a id="closebox" href="#" onclick="javascript:var conf = confirm('Are you sure you want to remove the login box? (It can be turned back on from settings)'); if(conf){ document.getElementById('onlinebar').style.display='none'; setBarPrefs('11769'); }" style="margin-right:20px;"><img src="/images/close.gif" border="0"/></a>

</div>

它的作用是获取 ID 为 tb_enemies 的元素中第一个 <b> 元素的文本内容,并将该文本解析为整数。