HugoBarjot / Country Flag Displayer

// ==UserScript==
// @name         Country Flag Displayer
// @description  Add a flag of the user country after username and/or a coat of arms under the username on Geoguessr user profile pages.
// @version      1.0.3
// @license      MIT
// @author       HugoBarjot#0332
// @match*
// @icon
// @require
// @downloadURL
// @updateURL
// @namespace
// @namespace
// @namespace
// ==/UserScript==

//You can check the full list of flags and coat of arms looks on this website :
//Be aware that some countries have no coat of arms available, u can check the full list here also :

//Replace true or false after '=' to enable or disable the set of  flags

//You can choose between 2 sets of flag wich will be displayed next to the username, make sure to replace the = value to true on only one between these 2 sets
const type_flag_normalflag_from_flagpedia = true   //flag from flagpedia api
const type_flag_normalflag_from_geoguessr = false  //flag from geoguessr api (similar to flagpedia)

//You can also choose to diplay a coat of arms of the country under the username. The width image size can be set and height is automatic to not disproportionate images. Replace value to false or true to disable or enable it. :)
const type_flag_coat_of_arms = true                //coat of arms from

//If u want to change the size of the coat of arms, u can lower or higher the value below (some coat of arms have height bigger than others because their shape differ). I found 120 value was the best overall look for me.
let size_coat_of_arm = 120

//If u want to add add a country that you can't pick from the geoguessr profile setttings page, you can replace below the value 'null' with the unicode you want use, be sur to add only 2 lowercase letters) example for antartica unicode : 'aq'
//full list of unicodes on this wiki page :
//After you change the unicode, save script and reload your profile page, you should see the flag of the country you wrote appear, once done no need to keep this unicode, replace it again to 'null' to not overwrite it everytime on your profile :)
//Examples : 'aq' for antartica, 'ad' for Ascension Island...
//Always replace text to 'null' after unicode is writen : 'null'
let your_unicode = 'null'

//Begin Code
let chosen_normal_flag = 'null';
let chosen_coat_of_arms = 'null';
const reg = /^[a-w]{2}/g;
let reg_test = null;
let unicode_change_url = ' '

function add_your_unicode(unicode){
    if (unicode !== 'null'){
        let reg_test = reg.test(unicode);
        if (reg_test === true){
            let promise = fetch(unicode_change_url, {
                method: 'PUT', body: `{"countryCode":"${unicode}"}`,
                headers: {
                "Content-Type": "application/json; charset=UTF-8" //pour un corps de type chaine
                credentials: 'include'
           window.alert("Unicode writen succesfully, Reload and make sure to replace again the unicode value to 'null' :)");
        if (reg_test === false ){
                window.alert("Make sure the first two letters of the unicode are in lowercase letters :)")
    else {

function check_normal_flag() {
    if (type_flag_normalflag_from_flagpedia === true && type_flag_normalflag_from_geoguessr === false){
        chosen_normal_flag = '';
    else if (type_flag_normalflag_from_geoguessr === true && type_flag_normalflag_from_flagpedia === false){
        chosen_normal_flag = '';
    else {
        window.alert("Make sure to check that only one type of normal flag is set to true on Country Flag Displayer script :)");

function check_coat_of_arms_flag() {
    if (type_flag_coat_of_arms === true){
       chosen_coat_of_arms = '';
    else {
        chosen_coat_of_arms = 'null';

let lastUrl = location.href;
new MutationObserver(() => {
  const url = location.href;
  if (url !== lastUrl && url.startsWith("") || url !== lastUrl && url.startsWith("")) {
    lastUrl = url;
}).observe(document, {subtree: true, childList: true});

function on_profile_page_change() {
    //console.log('Profile page detected!');

window.onload = function() {
    if (lastUrl.startsWith("")){
    const data = document.querySelectorAll("#__NEXT_DATA__")[0].text;
    const json = JSON.parse(data);
    const code_country = json.props.pageProps.user.countryCode;
    if (lastUrl.startsWith("")){
    const data = document.querySelectorAll("#__NEXT_DATA__")[0].text;
    const json = JSON.parse(data);
    const code_country = json.props.middlewareResults[0].account.user.countryCode;

    function add_flag(code_iso) {
      if (code_iso !== null){
      const img = document.createElement('img');
        if (chosen_normal_flag === ''){
            img.setAttribute('src', chosen_normal_flag+`/${code_iso.toUpperCase()}.svg`);
            img.setAttribute('src', chosen_normal_flag+`/${code_iso}.svg`);
        } = "flag";
      img.setAttribute('style', `margin-left:0.4rem;vertical-align: middle;border-radius:0.125rem`);
      img.setAttribute('alt', `country code : ${code_iso}`);
      const data_country = countryFlagEmoji.get(`${code_iso}`);
      const name_country_text = JSON.stringify(
      const regex_name = /"/g
      img.setAttribute('title', `${(name_country_text.replace(regex_name, ''))}`);
      img.setAttribute('width', 30);
      const svg_flag_location = document.querySelectorAll('h2[class="headline_heading__c6HiU headline_variantYellow__N_sDa"]')[0].firstChild;
        if (chosen_coat_of_arms === ''){
            let newDiv = document.createElement("div");
            newDiv.setAttribute("id", "coat_of_arms");
            newDiv.setAttribute('style', `height:auto;margin-bottom:1rem;`);
               if (lastUrl.startsWith("")){
                   const reference_node = document.querySelectorAll('div[class="profile_rank__i2Wyf"]')[0];
               if (lastUrl.startsWith("")){
                   const reference_node = document.querySelectorAll('div[class="slug_rankText__w4BvR"]')[0];
            const img_coat_of_arms = document.createElement('img');
            img_coat_of_arms.setAttribute('src', chosen_coat_of_arms+`/${code_iso}.svg`);
            img_coat_of_arms.setAttribute('width', size_coat_of_arm);
            img_coat_of_arms.setAttribute("onerror", "this.onerror=null; this.remove();");
            const svg_coat_of_arms_location = document.querySelector("#coat_of_arms");;