Code Monkey home page Code Monkey logo

Comments (15)

inexorabletash avatar inexorabletash commented on July 30, 2024

Hrm... as noted, the error "Encoder not present. Did you forget to include encoding-indexes.js?" indicates that the file was not included. But you do report you have a script tag for it.

Right before the encode call, can you try logging

console.log(typeof window['encoding-indexes']);
console.log(typeof window['encoding-indexes']['windows-1252']);

That should log 'object', 'object' if the indexes are present. If you get 'undefined', 'undefined', then somehow encoding-indexes.js is not being pulled in. If you get 'object', 'object' then there's a bug here somewhere. Can you try it and report back?

Removing window.TextEncoder = window.TextDecoder = null; means the polyfill will not be used so you're seeing the browser's error.

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

Doing

      console.log(typeof window['encoding-indexes']);
      console.log(typeof window['encoding-indexes']['windows-1252']);

Output is twice 'object'.
So there is a Bug somewhere?

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

Sounds like it. I'll try and repro but I'm traveling for a week so it'll be some time. You could try debugging in the index() function.

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

I have a guess - it's possible you've got some packaging that is wrapping the main function and this is not equal to the global scope.

Try replacing the last line in lib/encodings.js:

}(this));

with:

}(self));

... and make a similar change in lib/encoding-indexes.js

If that works I'll try and find a browser-friendly and node-friendly compromise. self references the global object in both window and worker contexts in the browser. Maybe this || self will do the trick - no sure. :(

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

Did it and no Changes at the moment

//encoding.js
...
  if (typeof module !== "undefined" && module.exports) {
    module.exports = {
      TextEncoder: global['TextEncoder'],
      TextDecoder: global['TextDecoder'],
      EncodingIndexes: global["encoding-indexes"]
    };
  }
}(self)); //last line
...
// encoding-indexes.js
...
  "windows-1258":[8364,129,8218,402,8222,8230,8224,8225,710,8240,138,8249,338,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,732,8482,154,8250,339,157,158,376,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,258,196,197,198,199,200,201,202,203,768,205,206,207,272,209,777,211,212,416,214,215,216,217,218,219,220,431,771,223,224,225,226,259,228,229,230,231,232,233,234,235,769,237,238,239,273,241,803,243,244,417,246,247,248,249,250,251,252,432,8363,255],
  "x-mac-cyrillic":[1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,8224,176,1168,163,167,8226,182,1030,174,169,8482,1026,1106,8800,1027,1107,8734,177,8804,8805,1110,181,1169,1032,1028,1108,1031,1111,1033,1113,1034,1114,1112,1029,172,8730,402,8776,8710,171,187,8230,160,1035,1115,1036,1116,1109,8211,8212,8220,8221,8216,8217,247,8222,1038,1118,1039,1119,8470,1025,1105,1103,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,8364]
}
;}(self)); //last line
....
console.log(typeof window['encoding-indexes']);
            console.log(typeof window['encoding-indexes']['windows-1252']);
            var uint8array = new TextEncoder(
              'windows-1252', { NONSTANDARD_allowLegacyEncoding: true }).encode(str);
              data = new Blob([str], {
              type: 'application/json;charset=windows-1252'
            });
          this.FileSaver.saveAs(data, this.$scope.selectedLang[i].code + '.php');

Still giving me 2 Objects -> So encoding.js is there
But saying

Encoder not present. Did you forget to include encoding-indexes.js?

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

Is it possible for you to host a sample anywhere publicly accessible that reproduces the problem?

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

Its is not running but there is the Code of my index.html and export.js
which should show the structere. I hope it helps a bit.
In index.html at Line:
56 script textencode...
96+97 script encode.js
export.js :
32 and down Textcode
http://plnkr.co/edit/HxFrOSMQI5fqmkrESF3R?p=catalogue

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

You need to include encoding-indexes.js before encoding.js - sorry, I should have spotted that sooner. I was looking at the wrong error message. :(

I'll try and add a check in the code that warns about that.

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

I should probably also note that pulling in the whole library and all indexes just to do windows-1252 is pretty crazy. You can always do this instead:

function encodeToWindows1252(string) {
  if (string.match(/[^\x00-\xFF]/)) throw new Error("Can't encode");
  return new Uint8Array(string.split('').map(function(c) { return c.charCodeAt(0); }));
}

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

Ok that Solved the Problem :)
Would be a nice Info for Future Users I think.

Another Question. I thought with this now it would be Formated to "windows-1252" Format.
But it is still Creating me a UTF-8 Encoded File?

 var uint8array = new TextEncoder(
              'windows-1252', { NONSTANDARD_allowLegacyEncoding: true }).encode(str);
              data = new Blob([uint8array], {
              type: 'application/json;charset=windows-1252'
            });
          this.FileSaver.saveAs(data, this.$scope.selectedLang[i].code + '.php');

Iam using
https://github.com/alferov/angular-file-saver
And somewhere there it was mentioned that they are Convert with your Package UTf-8 to ANSI Formats or equal. Is this right and again something went wrong or Iam wrong here? :)

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

Here it is mentioned that its used to Format it in other Codings than UTF
eligrey/FileSaver.js#28
But its still in UTF-8 ? :/

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

I'm not sure what your comment means.

from text-encoding.

atodicebear avatar atodicebear commented on July 30, 2024

I use this package(textencoding) to create a file in the windows 1252 format. Here(eligrey/FileSaver.js#28) it was mentioned that this is possible here. But the created file is still in UTf-8 format.

@b4stien not really the best place to talk about this, but I forked the text-encoding library just to achieve this (exporting Windows CP1252 CSV in JavaScript). See https://github.com/pmq/text-encoding and #19 for an explanation.

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

Here's a sample that works, encoding a string as windows-1252 and saving it:

<!DOCTYPE html>
<script src="https://rawgit.com/eligrey/FileSaver.js/master/FileSaver.js"></script>
<script>TextEncoder = TextDecoder = null</script>
<script src="https://rawgit.com/inexorabletash/text-encoding/master/lib/encoding-indexes.js"></script>
<script src="https://rawgit.com/inexorabletash/text-encoding/master/lib/encoding.js"></script>
<script>
function inspectString(title, s) {
  console.log(title + ': ' + s.split('').map(c => c.charCodeAt(0)));
}
function inspectArray(title, a) {
  console.log(title + ': ' + Array.from(a));
}
function inspectBlob(title, b) {
  const fr = new FileReader();
  fr.readAsArrayBuffer(b);
  fr.onload = () => {
    console.log(title + ': ' + Array.from(new Uint8Array(fr.result)));
  };
}

const str = '123ABCabc\xC0\xDF\xC7';
inspectString('code units', str);

const bytes = new TextEncoder(
  'windows-1252', { NONSTANDARD_allowLegacyEncoding: true }).encode(str);
inspectArray('bytes', bytes);

const blob = new Blob([bytes]);
inspectBlob('blob', blob);

saveAs(blob, 'file.txt');
</script>

The saved file ends up being windows-1252:

$ hexdump -C file.txt 
00000000  31 32 33 41 42 43 61 62  63 c0 df c7              |123ABCabc...|
0000000c

Try debugging your code to see where the problem is, inspecting the output at each step to ensure it is what you expect.

from text-encoding.

inexorabletash avatar inexorabletash commented on July 30, 2024

I updated the warning message.

from text-encoding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.