skip to Main Content

I am using a simple script on Tampermonkey to change some value on a webpage.

in the webpage, there is a input value on a input box i.e "FPPUTHP1100000"

This is a material code, The material code structure is as followes:-

F stand for FRESH

PPUTHP stand for PPC

110 Stand For ACS

0000 Stand for MRP.

Material code is not Universal, this could be changed each time.

i want to record it in variables and push a merged text in a empty input box

like ACS-PPC-MRP-FRESH

There are many material code but the basic structure is as above only.

First letter can be F,C,P,D (FRESH,C&T,PDMG,DMG)

Next six letter can be

PPUTHP,PPUSLH,PPUFLH,43UTHP,53UTHP (PPC,SUPER,WP,OPC43,OPC53)

next three letter can be 110,111,112,113,114 (ACS,VCS,PGU,DGU,AGU)

Next four letter can be 0000 and 5000 (MRP,NON MRP)

Can anybody tell how to make this script.

2

Answers


  1. first of all stor the value in variable by using document. getElementById()

    like this…

    var string = document.getElementById("*replace this with id if input field")
    

    now just apply substring functions on this string and save them into the variables like this…

    var first =  string.substring(1, 0); \ this will give you the first letter
    var second =  string.substring(1, 7); \ it will give you next 6 letters
    var third =  string.substring(7, 10);  \ it will give you next 3 letters
    var fourth =  string.substring(10, 14); \ it will give you next 4 letter
    

    now just apply conditions and make you string like this

     let final_string = ""
     if(first == "f"){
      final_string = final_string + "FRESH-"
     }
    
     else if(first == "C"){
      final_string = final_string + "C&T-"
     }
    
     else if(first == "P"){
      final_string = final_string + "PDMG-"
     }
    
     else{
      final_string = final_string + "DMG-"
     }
    

    now same apply if-else ladder and add the part to final_string accordingly..

    Login or Signup to reply.
  2. Assuming that the input is always in the order and length you’ve given, and the conversions are always one of the values you’ve given, you could create an array with the delimiting points to substring the input and then another array with matching indices as a lookup dictionary. Something like this:

    let delimits = [0,1,7,10],
        lookup = [
          { C: 'C&T', D: 'DMG', F: 'FRESH', P: 'PDMG' },
          { PPUTHP:'PPC', PPUSLH: 'SUPER', PPUFLH: 'WP', '43UTHP': 'OPC43', '53UTHP': 'OPC53' },
          { '110': 'ACS', '111': 'VCS', '112': 'PGU', '113': 'DGU', '114': 'AGU' },
          { '0000': 'MRP', '5000': 'NON MRP' }
        ];
    
    document.querySelector('button').addEventListener('click', function() {
      document.querySelector('input.output').value = delimits.map((delimit, index) =>
          lookup[index][document.querySelector('input.input')
            .value.substring(delimit, delimits[index + 1])]
      ).join('-');
    });
    
    document.querySelector('input.input').value = 'FPPUTHP1100000';
    <input class="input"><button> -> </button><input class="output">

    And if you want the output in a different order than the input, you can just:

    orig = output.split('-');
    reorder = [orig[2], orig[1], orig[3], orig[0]].join('-');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search