Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.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更换代码_Javascript_Html_Replace_Greasemonkey_Userscripts - Fatal编程技术网

Javascript Greasemonkey更换代码

Javascript Greasemonkey更换代码,javascript,html,replace,greasemonkey,userscripts,Javascript,Html,Replace,Greasemonkey,Userscripts,我想用如下元素中的openProfile替换openProfile: 我现在得到的是: textNodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); var searchRE = new RegExp('user.openProfileOrUnlockUser(item._type)','g'); var replace = 'user.

我想用如下元素中的
openProfile
替换
openProfile


我现在得到的是:

textNodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
var searchRE = new RegExp('user.openProfileOrUnlockUser(item._type)','g'); 
var replace = 'user.openProfile'; 
for (var i=0;i<textNodes.snapshotLength;i++) { 
    var node = textNodes.snapshotItem(i); 
    node.data = node.data.replace(searchRE, replace);
}
textNodes=document.evaluate(“//text()”,document,null,XPathResult.UNORDERED\u NODE\u SNAPSHOT\u TYPE,null);
var searchRE=new RegExp('user.openProfileOrUnlockUser(item.u type)','g');
var replace='user.openProfile';

对于(var i=0;i
ng),单击是一个属性,而不是一个文本节点,因此xpath将无法工作

显而易见的选择是
文档。querySelectorAll

[].forEach.call(document.querySelectorAll('[ng-click*="openProfileOrUnlockUser"]'), 
    function(node) {
        node.setAttribute('ng-click', 
            node.getAttribute('ng-click').replace('openProfileOrUnlockUser', 'openProfile')
        );
    }
);

不确定,但您可能需要转义regexp中的某些字符。请尝试使用这一行,而不是使用现有的
var searchRE=/user.openProfileOrUnlockUser\(item.\u type\)/g;
仅匹配节点的内容,而不是实际节点本身?如果我正确理解您的情况,您不需要正则表达式。只需应用替换
node.data.replace(“openProfile”或UnlockUser,“openProfile”);