0

Need to access html content from main code so that i can replace certain strings/words with another string and display in the web page.I have tried sending the array of strings from the main code to content script. Then i used jquery in the content script to get html contents. (.html()). I then replaced strings using javascript replace method and returned the new html content to load in web page. But it didnt work1 Need some help

Ram
  • 21
  • 3
  • At what point doesn't it work? Is there an error message in the web console or browser console? Please show the code that reinjects HTML into the page. Have you considered a GreaseMonkey script instead of a full addon? – Yolanda Ruiz Jan 26 '14 at 10:25
  • mainaddoncode: var tabs = require("sdk/tabs"); var self=require("sdk/self"); var pageMod=require("sdk/page-mod"); var str="to"; pageMod.PageMod({ include: "*", contentScriptWhen: "ready", contentScriptFile:[self.data.url("jquery-1.10.2.min.js"),self.data.url("script.js")], onAttach: function OnAttach(worker) { worker.port.emit("a",str); } }); – Ram Jan 26 '14 at 11:11
  • @YolandaRuiz script code: self.port.on("a",function(s1){ var s=s1; var h=$("body").html(); var h1; h1=h.replace(s,"FILTERED"); h=h1; $("body").html(h); }); – Ram Jan 26 '14 at 11:11
  • PLEASE JUST EDIT YOUR question with the example code, the question as-is is not enough information. – therealjeffg Jan 26 '14 at 21:19

1 Answers1

0

string .replace() method will only replace the first occurrence, try with this in your script code to replace all occurrences:

var h1 = h.replace(new RegExp(s, 'g'), 'FILTERED');

Please review this answer for a more complete treatment of replacing all, in case your input has special characters or user input values. Since the replacement uses a regex to operate, you must be careful with the input value, and escape it properly if there is a chance that it contains regex meta characters.

Yolanda Ruiz
  • 686
  • 4
  • 7