Search code examples
javascripthtmlreplacegreasemonkeyuserscripts

Greasemonkey replace code


I want to replace openProfileOrUnlockUser with openProfile in elements like this:

<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==">

What I have now is:

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

but it doesn't work.

P.S. searched for like 2 hours and haven't got it working.


Solution

  • ng-click is an attribute, not a text node so your xpath won't work.

    The obvious choice would be document.querySelectorAll:

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