I manage a forum that is hosted software so I don't have access to the source code, I can only add JavaScript to the page to achieve what I need done.
I'm trying to replace the first instance of certain text keywords on all pages with hyperlinks. I'm also geo-targeting those links based on country code.
I managed to piece together the code below by reading other answers on this site, and it actually works and does exactly what I want it to do, but it seems to be messing up other JavaScript (or jQuery?) Already loaded on the page previously.
Can anyone see anything wrong with this code that would cause it to affect other JavaScript code?
var c = null;
$.get("https://pro.ip-api.com/json/?key=XXXXXXXXXX", function(data) {
c = data.countryCode;
if (c == "GB") {
var thePage = $(".MessageList");
thePage.html(thePage.html().replace('KEYWORD1 ', '<a href="http://www.exampleGBlink1.com">KEYWORD1</a> '));
thePage.html(thePage.html().replace('KEYWORD2 ', '<a href="http://www.exampleGBlink2.com">KEYWORD2</a> '));
} else {
var thePage = $(".MessageList");
thePage.html(thePage.html().replace('KEYWORD1 ', '<a href="http://www.exampleELSElink1.com">KEYWORD1</a> '));
thePage.html(thePage.html().replace('KEYWORD2 ', '<a href="http://www.exampleELSElink2.com">KEYWORD2</a> '));
}
console.log(data.countryCode);
}, "jsonp");
Another problem I have with this code: it's also replacing instances of the keywords in my HTML code, so I added the extra blank space at the end of the keywords as a hack around this.
EDIT:
If I add the code below underneath the code above then everything works properly and my problems are gone, what gives? This file doesn't exist and I get a console error: "Failed to load resource: net::ERR_INSECURE_RESPONSE"
<script src="https://www.RandomDomain.com/test.js"></script>