Advertisement
Guest User

Untitled

a guest
Jun 1st, 2012
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Correspondance Windows-1252 -> utf-8 des caractères 128 à 255.
  3.  * Chaque index contient le code utf-8 du 128ème caractère après lui.
  4.  */
  5. var COWARD_CHARS = [
  6.     '%E2%82%AC', '%C2%81', '%E2%80%9A', '%C6%92', '%E2%80%9E', '%E2%80%A6', '%E2%80%A0', '%E2%80%A1', '%CB%86', '%E2%80%B0', '%C5%A0', '%E2%80%B9', '%C5%92', '%C2%8D', '%C5%BD', '%C2%8F',
  7.     '%C2%90', '%E2%80%98', '%E2%80%99', '%E2%80%9C', '%E2%80%9D', '%E2%80%A2', '%E2%80%93', '%E2%80%94', '%CB%9C', '%E2%84%A2', '%C5%A1', '%E2%80%BA', '%C5%93', '%C2%9D', '%C5%BE', '%C5%B8',
  8.     '%C2%A0', '%C2%A1', '%C2%A2', '%C2%A3', '%C2%A4', '%C2%A5', '%C2%A6', '%C2%A7', '%C2%A8', '%C2%A9', '%C2%AA', '%C2%AB', '%C2%AC', '%C2%AD', '%C2%AE', '%C2%AF',
  9.     '%C2%B0', '%C2%B1', '%C2%B2', '%C2%B3', '%C2%B4', '%C2%B5', '%C2%B6', '%C2%B7', '%C2%B8', '%C2%B9', '%C2%BA', '%C2%BB', '%C2%BC', '%C2%BD', '%C2%BE', '%C2%BF',
  10.     '%C3%80', '%C3%81', '%C3%82', '%C3%83', '%C3%84', '%C3%85', '%C3%86', '%C3%87', '%C3%88', '%C3%89', '%C3%8A', '%C3%8B', '%C3%8C', '%C3%8D', '%C3%8E', '%C3%8F',
  11.     '%C3%90', '%C3%91', '%C3%92', '%C3%93', '%C3%94', '%C3%95', '%C3%96', '%C3%97', '%C3%98', '%C3%99', '%C3%9A', '%C3%9B', '%C3%9C', '%C3%9D', '%C3%9E', '%C3%9F',
  12.     '%C3%A0', '%C3%A1', '%C3%A2', '%C3%A3', '%C3%A4', '%C3%A5', '%C3%A6', '%C3%A7', '%C3%A8', '%C3%A9', '%C3%AA', '%C3%AB', '%C3%AC', '%C3%AD', '%C3%AE', '%C3%AF',
  13.     '%C3%B0', '%C3%B1', '%C3%B2', '%C3%B3', '%C3%B4', '%C3%B5', '%C3%B6', '%C3%B7', '%C3%B8', '%C3%B9', '%C3%BA', '%C3%BB', '%C3%BC', '%C3%BD', '%C3%BE', '%C3%BF'
  14. ];
  15. /**
  16.  * Correspondance cp1252 -> iso-8859-1
  17.  * Chaque sous-array a des index contenant le code iso-8859-1 des index cp1252 marqués en commentaires.
  18.  */
  19. var EXTREMELY_COWARD_CHARS_SOLUTIONS = [
  20.     ['%8A', '%8B', '%8C', '%8D', '%8E', '%8F'], // 138 .. 143
  21.     ['%9A', '%9B', '%9C', '%9D', '%9E', '%9F'], // 154 .. 159
  22.     ['%C9', '%CA', '%CB', '%CC', '%CD', '%CE', '%CF'], // 201 .. 207
  23.     ['%D9', '%DA', '%DB', '%DC', '%DD', '%DE', '%DF']  // 217 .. 223
  24. ];
  25.  
  26. function escapeURIComponent(str) {
  27.     str = encodeURIComponent(str);
  28.    
  29.     var i, pkmn, len, index, evolution;
  30.     for (i = 0; i < str.length; i++) {
  31.         if (str[i] == '%') { // un caractère spécial apparaît
  32.        
  33.             if (str[i + 1] == 'E') { // le caractère prend 3 bits
  34.                 len = 9; // %xx%xx%xx
  35.             }
  36.             else { // 2 bits
  37.                 len = 6; // %xx%xx
  38.             }
  39.            
  40.             pkmn = str.substr(i, len);
  41.            
  42.             index = COWARD_CHARS.indexOf(pkmn);
  43.             if (index != -1) {
  44.                 index += 128;
  45.                 if ((index >= 138 && index <= 143)
  46.                 || (index >= 154 && index <= 159)
  47.                 || (index >= 201 && index <= 207)
  48.                 || (index >= 217 && index <= 223)) {
  49.                     if (index <= 143) {
  50.                         evolution = EXTREMELY_COWARD_CHARS_SOLUTIONS[0][index - 138];
  51.                     }
  52.                     else if (index <= 159) {
  53.                         evolution = EXTREMELY_COWARD_CHARS_SOLUTIONS[1][index - 154];
  54.                     }
  55.                     else if (index <= 207) {
  56.                         evolution = EXTREMELY_COWARD_CHARS_SOLUTIONS[2][index - 201];
  57.                     }
  58.                     else if (index <= 223) {
  59.                         evolution = EXTREMELY_COWARD_CHARS_SOLUTIONS[3][index - 217];
  60.                     }
  61.                     else {
  62.                         showError('EXTREMELY_COWARD_CHARS_SOLUTIONS not found');
  63.                     }
  64.                 }
  65.                 else {
  66.                     evolution = index.toString(16);
  67.                     if (evolution.length == 1) {
  68.                         evolution = '0' + evolution;
  69.                     }
  70.                     evolution = '%' + evolution;
  71.                 }
  72.                
  73.                 str = str.substr(0, i) + evolution + str.substr(i + len);
  74.             }
  75.         }
  76.     }
  77.     return str;
  78. }
  79.  
  80. // USAGE:
  81. var xhr = new XMLHttpRequest();
  82. xhr.open('POST', '/cgi-bin/jvforums/forums.cgi', true);
  83. xhr.send('yournewmessage='+escapeURIComponent(geid('area').value));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement