我想在这样的元素中用 openProfile
替换 openProfileOrUnlockUser
:
<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);
}
但它不起作用。
附言搜索了大约 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')
);
}
);
关于javascript - Greasemonkey 替换代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782070/