Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Greasemonkey脚本中未定义innerText_Javascript_Greasemonkey_Scope - Fatal编程技术网

Javascript Greasemonkey脚本中未定义innerText

Javascript Greasemonkey脚本中未定义innerText,javascript,greasemonkey,scope,Javascript,Greasemonkey,Scope,我编写了查询文档中匹配字符串的代码,并根据获得的字符串生成URL。它通过标记元素查找匹配项,生成URL字符串,然后将链接附加到指定的parentNode对象。这段代码在普通javascript中运行良好,但当我将其粘贴到Greasemonkey中时,它就会崩溃。我不明白为什么 当我将其粘贴到chrome控制台中时,这是一个完全工作的版本: //loop through elements by classname and find string matches regexQueryEmail =

我编写了查询文档中匹配字符串的代码,并根据获得的字符串生成URL。它通过标记元素查找匹配项,生成URL字符串,然后将链接附加到指定的
parentNode
对象。这段代码在普通javascript中运行良好,但当我将其粘贴到Greasemonkey中时,它就会崩溃。我不明白为什么

当我将其粘贴到chrome控制台中时,这是一个完全工作的版本:

//loop  through elements by classname and find string matches
regexQueryEmail = "(AccountEmailAddress\\s)(.+?)(\\n)"
regexQueryContact = "(Contact with ID: )(.+?)(\\D)"

var Tags = document.getElementsByClassName('msg-body-div')
for (i = 0; i < Tags.length; i++) {
    matchEmail = Tags[i].innerText.match(regexQueryEmail)
    matchContact = Tags[i].innerText.match(regexQueryContact)
    if (matchEmail != null) {
        var emailString = matchEmail[2]
        var placeHolder = Tags[i]
    }
    if (matchContact != null) {
        var idString = matchContact[2]
    }
}

var urlFirst = "https://cscentral.foo.com/gp/stores/www.foo.com/gp/communications/manager/main/191- 4559276-8054240?ie=UTF8&customerEmailAddress="
var urlSecond = "%3E&initialCommId="
var cscURL = urlFirst + emailString + urlSecond + idString

var cscLink = document.createElement('a')
cscLink.innerText = 'Communication History'
cscLink.href = cscURL
placeHolder.parentNode.appendChild(cscLink)


它还告诉我“占位符”未定义,但我现在无法复制它。我觉得这与变量的作用域有关。我在脚本的顶部添加了“var标签”和“var占位符”,但没有任何帮助

Firefox使用
元素.textContent
属性

中的变量
占位符
从未在尝试使用它的范围内声明。相反,它是在
for
循环中的某个地方声明的。确保在同一范围内声明它

例如

var Tags=document.getElementsByClassName('msg-body-div'))
变量占位符;//在同一范围内声明
对于(var i=0;i
/*
    Exception: Tags[i].innerText is undefined
    @Scratchpad:18
*/
var Tags = document.getElementsByClassName('msg-body-div')

var placeholder; // declare in same scope

for (var i = 0; i < Tags.length; i++) {

    // lookup the tag once
    var tag = Tags[i];

    // get the text only once
    var text = tag.textContent;

    matchEmail = text.match(regexQueryEmail)
    matchContact = text.match(regexQueryContact)

    if (matchEmail != null) {
        var emailString = matchEmail[2]

        placeHolder = tag // deleted var statement
    }
    if (matchContact != null) {
        var idString = matchContact[2]
    }
}

...

// now you can use it.

if (placeHolder) {
    placeHolder.parentNode.appendChild(cscLink);
}