未定义变量时继续脚本

时间:2018-12-09 00:48:16

标签: javascript tampermonkey

我有一个在tampermonkey中运行的脚本,该脚本根据中的文本查找表的特定列,并定义该列中每个单元格的所有内容。

它工作正常,当我转到另一个我也打算运行此脚本的页面并且我在第一个表上搜索的标题在另一个表上不存在时,问题就来了。但是,仍然有两列相同。我在第二张桌子上收到此错误。

ERROR: Execution of script 'scriptName' failed! titleTh is undefined

是否可以在脚本中添加一些内容,以便即使未定义变量也可以继续运行?

定义th就像这样...

var allTh = document.querySelectorAll('th');
var titleTh = Array.from(allTh).find(th => th.innerText === 'Title of the th'); //this is where it fails because the second table doesn't have the one column that the first table has.

0 个答案:

没有答案