Greasemonkey 替换代码

Greasemonkey replace code

我想在这样的元素中将 openProfileOrUnlockUser 替换为 openProfile

<a ng-href="/profile/CRYJIEjJGv+th/a/aQmDoFisAhE+LSldMLJbk1VCTJUJMeCqzFw0xZb9Q=="
   ng-click="user.openProfileOrUnlockUser(item._type)" 
   class="show overflow-hidden absolute-fill" 
   eat-click="" 
   href="/profile/CRYJIEjJGv+th/a/aQmDoFisAhE+LSldMLJbk1VCTJUJMeCqzFw0xZb9Q==">

我现在拥有的是:

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);
}

但是没用。

P.S。搜索了大约 2 个小时,但没有成功。

ng-click 是一个属性,而不是文本节点,因此您的 xpath 将不起作用。

显而易见的选择是 document.querySelectorAll:

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