简体   繁体   中英

Issues with ActiveXObject in Angular 7

I am developing a small page/router component on a website using Angular 7 and its CLI. At one point I need to check if the user has allowed flash, I do this by doing so:

checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
    hasFlash = "undefined" != typeof navigator.mimeTypes["application/x-shockwave-flash"];
  }

  return hasFlash;
}

I found this off here , and it works great, but now as I am cleaning up my application I am noticing that Angular doesn't seem to like this, in fact, it says that ActiveXObject isn't defined, yet it still works.

Super confused...

I tried linking an actual flash object like so $('embed[type="application/x-shockwave-flash"]') or $('embed[type="application/x-shockwave-flash"]')[0] but had no luck, it always returned true.

I tried installing extra npm (s) including ones like activex-support and activex-data-support as well as their @types cousins. After setting them up I found out that they did nothing to help my case.

Here are the exact errors the CLI & VScode - Intellisense gave me:

VScode:

[ts] 'ActiveXObject' only refers to a type, but is being used as a value here. [2693] any

CLI:

ERROR in src/app/games/games.component.ts(51,30): error TS2304: Cannot find name 'ActiveXObject'.

It doesn't throw this error when ran inside plain JS, but I've looked around and can't seem to figure out how to run pure JS inside Angular 2 (7) . Also looked here with no luck.

Please help, completely lost here.

EDIT: Found the fix --> The answer was here listed inside the comments (will need to make minor changes)(shown below)

  • change from:
checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x- 
shockwave-flash"];
  }

  return hasFlash;
}
  • to:
function checkFlash() {
    var hasFlash = false;
    try {
        var flash =
            navigator.mimeTypes &&
            navigator.mimeTypes["application/x-shockwave-flash"]
                ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
                : 0;
        if (flash) hasFlash = true;
    } catch (e) {
        if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
            hasFlash = true;
    }

    return hasFlash;
}

The answer was here listed inside the comments (will need to make minor changes)(shown below)

change from:

checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x- 
shockwave-flash"];
  }

  return hasFlash;
}

to:

function checkFlash() {
    var hasFlash = false;
    try {
        var flash =
            navigator.mimeTypes &&
            navigator.mimeTypes["application/x-shockwave-flash"]
                ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
                : 0;
        if (flash) hasFlash = true;
    } catch (e) {
        if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
            hasFlash = true;
    }

    return hasFlash;
}

This issue with ActiveXObject can be solve as shown below: Goto your tsconfig.json file and add the ' scripthost ' library. Then recompile your application.

 "lib": [
  "es2017",
  "dom",
  "scripthost"
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM