Search code examples
javascriptbookmarklet

Javascript text search and replace don't work on bookmarklet


I highlight the following text with mouse:

Site: http://www.solidfiles.com
Sharecode[?]: /d/1234567890

Then use the following bookmarklet commands:

javascript:(function(){
  var txt = window.getSelection();
  txt = txt.replace('[?]', '');
  txt = txt.replace('Site: ', '');
  txt = txt.replace('Sharecode: ', '');
  txt = txt.replace(/\n/g, '');
  window.open(txt);
})();

The javascript stop working when use txt.replace commands. I don't know what are wrong. Please help.


Solution

  • window.getSelection() return a Selection object, you need to stringify it before: txt = txt.toString();.