Greasy Fork is available in English.

Συζητήσεις » Ανάπτυξη

Platform detection using switch/case

§
Δημοσιεύτηκε: 27/06/2023

Hello,

How can I implement the following code with switch () { case: }

  if (navigator.platform.toLowerCase().includes('ubuntu'))  {
        document.querySelector('#ubports').classList.add('background');
  } else
  if (navigator.platform.toLowerCase().includes('tizen'))  {
        document.querySelector('#tizen').classList.add('background');
  } else
  if (navigator.platform.toLowerCase().includes('sailfish'))  {
        document.querySelector('#sailfish-os').classList.add('background');
  } else
  if (navigator.platform.toLowerCase().includes('kai'))  {
        document.querySelector('#gerda-os').classList.add('background');
  } else
  if (navigator.platform.toLowerCase().includes('linux') ||
      navigator.platform.toLowerCase().includes('bsd')) {
        document.querySelector('#unix').classList.add('background');
  }
§
Δημοσιεύτηκε: 27/06/2023
Επεξεργάστηκε: 27/06/2023

Bro pls use AI first, they are your friend and they aren't looking for world domination yet...

Then if that does not help you can come here

https://chat.openai.com/share/f598b73c-7194-4e19-86f8-8e8a07da539e

§
Δημοσιεύτηκε: 27/06/2023

To me, my code looks clumsy (also the one suggested by the ai).

I just wanted something shorter and cleaner. I think I'll use indexOf and read from navigator.userAgent.

I didn't ask the ai first. I'll do so from now on.

Thank you

§
Δημοσιεύτηκε: 27/06/2023

oh I see, that is a totally different question then

Maybe try
if (navigator.platform.toLowerCase().includes('tizen, ubuntu, sailfish, kai, linux, bsd')) { //you may ahve to use regex match instead
document.querySelector('#tizen, #ubports, #sailfish-os, #gerda-os, #unix').classList.add('background');
}

§
Δημοσιεύτηκε: 28/06/2023

I'm sorry, I didn't clarify the purpose of this code.

In the Help page, the script provides a list of Synducation-supported applications for all platforms.

The list is fixed and it is intended that everyone from all platforms will see the list at its entirety.

Because of this, I made the above code to highlight the section of the detected platform.

I guess I can use two arrays or json (dictionary-style).

§
Δημοσιεύτηκε: 28/06/2023
Επεξεργάστηκε: 28/06/2023

ChatGPT(GPT-3.5) Answer

const matches = [
  {
    platform: 'ubuntu',
    id: '#ubports',
    class: 'background'
  },
  {
    platform: 'tizen',
    id: '#tizen',
    class: 'background'
  },
  {
    platform: 'sailfish',
    id: '#sailfish-os',
    class: 'background'
  },
  {
    platform: 'kai',
    id: '#gerda-os',
    class: 'background'
  },
  {
    platform: 'linux',
    id: '#unix',
    class: 'background'
  },
  {
    platform: 'bsd',
    id: '#unix',
    class: 'background'
  }
];

const platform = navigator.platform.toLowerCase();

for (const match of matches) {
  if (platform.includes(match.platform)) {
    document.querySelector(match.id).classList.add(match.class);
    break;
  }
}
§
Δημοσιεύτηκε: 28/06/2023

Thank you!

NotYouΣυντονιστής
§
Δημοσιεύτηκε: 30/06/2023

Bro pls use AI first, they are your friend and they aren't looking for world domination yet...

That is what AI would say.

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.