Hi i want to create a chrome extension that will automatically replace a div with a specific class to another div.There is an online CAD software called Ondrive and i want to see a modified version of its menu.
Note the div to be replaced and the resulting will contain several more elements inside (so a lot of markup like divs, svg images, text and links)
There is a nice tutorial in the link below to replace text, but i don't know how to modify it for something that complex
https://9to5google.com/2015/06/14/how-to-make-a-chrome-extensions/
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(/[word or phrase to replace here]/gi, '[new word or phrase]');
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
I modified it to be
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(/<div command-id="extrude" data-id="5978b990310f18a3a66ada29" data-placement="bottom" data-original-title="Extrude (Shift-e)" data-expanded-content="" data-html="true" data-expand-delay="2000" tooltip-dynamic-snippet-id="" class="tool is-activatable is-button" data-original-expanded-content="Create, add to, subtract from, or intersect parts by extruding sketch regions or planar faces, or surfaces by extruding lines or curves.<ol><li>Select regions or faces, or sketch edges.</li><li>Specify whether to create a new part or surface, add, remove, or intersect with existing parts</li><li>Specify the distance or the entity to extrude up to.</li></ol>"><!----> <div><svg xmlns="http://www.w3.org/2000/svg" class="os-svg-icon"><use xlink:href="#svg-icon-extrude-button"></use></svg></div> <span class="tool-label hide-in-toolbar"> Extrude </span> <!----> <!----></div>/gi, '<div command-id="revolve" data-id="5978b990310f18a3a66ada2a" data-placement="bottom" data-original-title="Revolve" data-expanded-content="" data-html="true" data-expand-delay="2000" tooltip-dynamic-snippet-id="" class="tool is-activatable is-button" data-original-expanded-content="Create, add to, subtract from, or intersect parts by revolving sketch regions or planar faces about a central axis, or surfaces by revolving lines and curves about a central axis.<ol><li>Select one or more sketch regions, faces, or edges.</li><li>Specify whether to create a new part or surface, and to add, remove, or intersect with existing parts.</li><li>Select a sketch line or edge as the axis to revolve about.</li></ol>"><!----> <div><svg xmlns="http://www.w3.org/2000/svg" class="os-svg-icon"><use xlink:href="#svg-icon-revolve-button"></use></svg></div> <span class="tool-label hide-in-toolbar"> Revolve </span> <!----> <!----></div>');
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
This didn't work at all. There is a hashtag # after which the code is invalid (but i have to use). Also i am not sure if this is the most elegant solution to replace code