Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Had this written up for your last question just before it was deleted.</p> <p>Here are the problems you're having with your GM script.</p> <ol> <li><p>You're checking absolutely every text node on the page for some reason. This isn't causing it to break but it's unnecessary and slow. It would be better to look for text nodes inside .price nodes and .rrp .strike nodes instead.</p></li> <li><p>When creating new regexp objects in this way, backslashes must be escaped, ex: </p> <p><strong>var searchRE = new RegExp('\\d\\d','gi');</strong> </p> <p>not</p> <p><strong>var searchRE = new RegExp('\d\d','gi');</strong></p> <p>So you can add the backslashes, or create your regex like this: </p> <p><strong>var searchRE = /\d\d/gi;</strong></p></li> <li><p>Your actual regular expression is only checking for numbers like ##ANYCHARACTER##, and will ignore £5.00 and £128.24</p></li> <li><p>Your replacement needs to be either a string or a callback function, not a regular expression object.</p></li> </ol> <hr> <h2>Putting it all together</h2> <pre><code>textNodes = document.evaluate( "//p[contains(@class,'price')]/text() | //p[contains(@class,'rrp')]/span[contains(@class,'strike')]/text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); var searchRE = /£(\d+\.\d\d)/gi; var replace = function(str,p1){return "₪" + ( (p1*5.67).toFixed(2) );} for (var i=0,l=textNodes.snapshotLength;i&lt;l;i++) { var node = textNodes.snapshotItem(i); node.data = node.data.replace(searchRE, replace); } </code></pre> <p>Changes:</p> <ul> <li>Xpath now includes only p.price and p.rrp span.strke nodes</li> <li>Search regular expression created with /regex/ instead of new RegExp</li> <li>Search variable now includes target currency symbol</li> <li>Replace variable is now a function that replaces the currency symbol with a new symbol, and multiplies the first matched substring with substring * 5.67</li> <li>for loop sets a variable to the snapshot length at the beginning of the loop, instead of checking textNodes.snapshotLength at the beginning of every loop.</li> </ul> <p>Hope that helps!</p> <p>[edit]Some of these points don't apply, as the original question changed a few times, but the final script is relevant, and the points may still be of interest to you for <em>why</em> your script was failing originally.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload