代码之家  ›  专栏  ›  技术社区  ›  Ahmed Shams

节点gyp c++chrome扩展

  •  0
  • Ahmed Shams  · 技术社区  · 4 年前

    我需要创建chrome扩展,它可以从js调用c++,所以我使用 nodeapi 为了实现这一点,我使用 节点gyp

    package.json

    {
      "name": "test",
      "version": "0.13.0",
      "description": "test",
      "main": "./index.js",
      "files": [
        "index.js"
      ],
      "engines": {
        "node": ">=12.0.0"
      },
      "scripts": {
        "test": "node index.js",
        "build": "node-gyp rebuild",
        "clean": "node-gyp clean",
        "install": "prebuild-install --runtime napi || node-gyp rebuild"
      },
      "dependencies": {
        "bindings": "^1.5.0",
        "browserify": "^17.0.0"
        "node-addon-api": "^3.0.2",
        "prebuild-install": "^6.0.0"
      },
      "devDependencies": {
        "babel-eslint": "^10.1.0",
        "chai": "^4.1.2",
        "chai-spies": "^1.0.0",
        "eslint": "^6.8.0",
        "eslint-plugin-import": "^2.20.1",
        "eslint-plugin-json": "^1.2.0",
        "eslint-plugin-mocha": "^6.2.2",
        "mocha": "^5.0.4",
        "node-gyp": "^9.1.0",
        "prebuild": "^10.0.1",
        "rimraf": "^2.6.2"
      },
      "gypfile": true
    }
    

    装订gyp

    {
      "targets": [
        {
            'target_name': 'hello',
            'sources': [ 'hello.cc' ],
            'defines': [
                '_LARGEFILE_SOURCE',
                '_FILE_OFFSET_BITS=64',
                'NAPI_DISABLE_CPP_EXCEPTIONS'
            ],
            'cflags!': ['-ansi', '-fno-exceptions' ],
            'cflags_cc!': [ '-fno-exceptions' ],
            'cflags': ['-g', '-exceptions'],
            'cflags_cc': ['-g', '-exceptions'],
            'include_dirs': [
              "<!@(node -p \"require('node-addon-api').include\")"
            ],
            'dependencies': [
              "<!(node -p \"require('node-addon-api').gyp\")"
            ],
        }, 
      ]
    }
    

    你好.cc

    #include <napi.h>
    Napi::String Method(const Napi::CallbackInfo& info) {
      Napi::Env env = info.Env();
      return Napi::String::New(env, "Hello Calling From Cpp");
    }
    
    Napi::Object Init(Napi::Env env, Napi::Object exports) {
      exports.Set(Napi::String::New(env, "hellocc"), Napi::Function::New(env, Method));
      return exports;
    }
    
    NODE_API_MODULE(hello, Init)
    

    index.js

     'use strict'
     var addon = require('bindings')('hello');
     function testingfromjs(){
         return addon.hellocc();
     }
    exports.testingfromjs= testingfromjs;
    

    问题是它可以很好地与以下命令配合使用 node index.js 但当它作为chrome扩展构建时,它就不起作用了 我将它作为依赖项导入,并根据需要在chrome扩展的main.js中使用它,但它不起作用

    0 回复  |  直到 4 年前
    推荐文章