3
I’m trying to develop an extension for Chrome, and I’m still in the early stages, and at the very beginning I’m already stuck. I know very little about javascript but I try to do what I can by searching. This extension is inspired by Foxreplace of firefox, ie, is an extension that searches for all the entries designated by the user on the page that is and replaces by any other text that the user put.
I found an extension for Chrome on github and I used it as a reference, but because it’s hyper simple (just take "The Cloud" and replace it with "my Butt" -- yes, it’s very crude), I can’t follow much, besides that the code written there I still can’t interpret very well to put what I want.
What I’ve tried (from scratch):
Placed:
var str = "Texto da substituição";
var res = str.replace(/Será substituído/gim,"Substituirá");
But it doesn’t work the way I wanted it to, because in addition to only searching for the term within "Replacement Text", instead of the whole document, it also needs something to start the search by the terms, which I searched on several websites, but all I found was codes to put directly into the page’s HTML, in the tag body
, which I think, if I’m not wrong, would not have contact with the extension, by being part of the page and not the application.
The extension I picked up uses walk(document.body)
and then sets the function walk(node)
with a "statement" (I don’t know how this is called in Portuguese) switch (node.nodeType)
, which, from what I’ve seen, works as expected, but needs several fixes because it fails in several different situations.
So I’d like someone to explain to me how walk
, of switch
and of case
s the way they were used, and/or tell me how to do it that I intend in a more appropriate way for my intention.
This extension that I picked up, having edited with what I know, works until well, the problem are the bugs and some things kinda inexplicable that happen with his main code, that I will only be able to, maybe, correct, when I understand the content.
Something I can’t understand is why this script doesn’t work on
iframe
s, for example, the comments of the Facebook plugin. If the 4°case
serves for any visible page text, because loads of water he can’t see the text inside theiframe
?? I’ve tried every line of codeiframe
To see if I could find the terms inside him, but nothing worked.– Yuuza
@Brunolopes From the DOM of the main page, you cannot access the content of
iframe
s - especially from a different domain - for security reasons. Being a browser extension, maybe it has additional privileges, I don’t know... I updated my answer with more details.– mgibsonbr