I’m not a skillful coder, that’s why my explanation is going to be easy and basic.
I want to make a personal Google Chrome extension that makes HTML changes in a web page i don’t own. I don’t care if it’s non-optimized or ugly code. I just want to make it work. Write a lot, don’t fear me.
The code have to detects the variable “title=”xxxx”, and then it will add a change to the code.
For example, if:
<h2>
<a href="/car/bmw" class="secondaryPageLink" title="BMW's page">BMW</a>
</h2>
<div class="age">
<p>2005</p>
Then it’s replaced with:
<h2>
<a href="/car/bmw" class="secondaryPageLink" title="BMW's page">BMW</a>
</h2>
<div class="age">
<p>250000,00$</p>
<p>2005</p>
if:
<h2>
<a href="/car/mercedes" class="secondaryPageLink" title="Mercedes's page">Mercedes</a>
</h2>
<div class="age">
<p>1987</p>
Then it’s replaced with:
<h2>
<a href="/car/mercedes" class="secondaryPageLink" title="Mercedes's page">Mercedes</a>
</h2>
<div class="age">
<p>750000,00$</p>
<p>1987</p>
At the very least you will need the following files:
manifest.json
contentscript.js
Here’s a jsfiddle that works.
Place both of these files into a folder and use the “load unpacked extension” button in Chrome.
I guess it’s fair to say: warning, this is a very crude solution. It’s ugly and will probably break in some cases, but you said you don’t care about code quality… so here it is.
Also, the HTML code you provide is invalid. It should be:
And hopefully it gets closed with
</div>somewhere later in the code.EDIT #1
Okay, you’ve updated the question. Try putting the following in the contentscript.js:
Here’s the updated jsfiddle example.
EDIT #2
Try looking at the code provided above and reading documentation on various functions that are used. You will never learn otherwise…
If you read documentation on querySelector and selectors you will figure out that to find a title node you can modify the above code as follows:
… or to find by pure brand name that can be found in the
hrefattribute:Then to find the age node, you can look up its parent and the next element of the parent:
Here’s a jsfiddle example that demonstrates it.
Also, once again your HTML is incorrect. You haven’t properly closed the
<h2></h2>tags.