简体   繁体   English

如何在Gjs Gnome Javascript中包含文件

[英]How can I include files with Gjs Gnome Javascript

I understand I can import files easily enough, but I'm trying to make some kind of plugin structure. 我知道我可以很容易地导入文件,但是我正在尝试制作某种插件结构。 I'd like to be able scan through a folder and load each Javascript file. 我希望能够浏览一个文件夹并加载每个Javascript文件。

With Seed JS I can use Seed.include() to evalute a file as if it were included in the file at the point include is called. 使用Seed JS,我可以使用Seed.include()评估文件,就像在调用include时将其包含在文件中一样。 ( reference ) 参考

Does Gnome Javascript (Gjs) have an equivalent function? Gnome Javascript(Gjs)是否具有等效功能?

There is a thread about this problem, the guy apparently complains that it doesn't actually work. 关于这个问题有一个线索 ,那个家伙显然抱怨说它实际上是行不通的。 I don't know if it worth trying because anyway the approach sucks big way - it too cumbersome in my opinion. 我不知道是否值得尝试,因为无论如何该方法很麻烦-我认为这太麻烦了。

So I'll just share what I did in my gnome extension. 因此,我将只分享我在gnome扩展程序中所做的工作。

TL;DR: Use Node + Webpack to have nice module system and access to huge library of modules. TL; DR:使用Node + Webpack拥有不错的模块系统并访问庞大的模块库。

  • First of all I've installed Node + npm because together they give you quite capable module system. 首先,我安装了Node + npm,因为它们一起为您提供了功能强大的模块系统。 Bonus - you can use public NPM modules if they don't use any Node API. 奖励-如果公共NPM模块不使用任何Node API,则可以使用它们。
  • I've created package.json file, here it is: 我已经创建了package.json文件,它是:
{
  "name": "blah",
  "version": "0.0.1",
  "description": "blah",
  "scripts": {
    "watch": "nodemon --exec 'npm run build'",
    "build": "webpack"
  },
  "author": "me",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^1.11.0",
    "webpack": "^2.2.1"
  },
  "dependencies": {
    "string-format": "^0.5.0"
  }
}
  • Next, I've created my webpack.config.js file: 接下来,我创建了webpack.config.js文件:
var path = require('path');

module.exports = {
  entry: {
        main: './src/main.js',
        ui: './src/ui.js'
    },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname),
        libraryTarget: 'var',
        library: '[name]'
  },
    resolve: {
        modules: [
            path.resolve('./src'),
            'node_modules'
        ]
    },
    externals: {
        'gnome': 'global',
        'lang': 'imports.lang',
        'gi/meta': 'imports.gi.Meta',
        'gi/shell': 'imports.gi.Shell',
        'ui/main': 'imports.ui.main',
        'ui/popupMenu': 'imports.ui.popupMenu',
        'ui/panelMenu': 'imports.ui.panelMenu',
        'gi/atk': 'imports.gi.Atk',
        'gi/st': 'imports.gi.St',
        'gi/gtk': 'imports.gi.Gtk',
        'gi/gdk': 'imports.gi.Gdk',
        'gi/gobject': 'imports.gi.GObject',
        'gi/gio': 'imports.gi.Gio',
        'gi/soup': 'imports.gi.Soup',
        'gi/glib': 'imports.gi.GLib',
        'gi/clutter': 'imports.gi.Clutter',
        'misc/config': 'imports.misc.config',
        'me': 'imports.misc.extensionUtils.getCurrentExtension()'
    }
};

So now I get to use Node's module system and I can just require my stuff into files. 所以现在我开始使用Node的模块系统,我只需要将我的东西放入文件即可。 I then run npm run build and webpack gives me one nice tight file which I can run with all of the stuff required inside. 然后,我运行npm run build并且webpack给了我一个不错的紧凑文件,我可以运行其中所需的所有内容。

Have an example of a file: 有一个文件示例:

const Lang = require('lang')
const Gdk = require('gi/gdk')

const parser = require('./parser')
const modifier = require('./modifier')

const rules = [
    parser((state, result) => {
        const name = result.string
        const rule = new RegExp('^[a-zA-Z0-9]+$')
        return name && rule.test(name)
    }, '', true),
    modifier(Gdk.ModifierType.SUPER_MASK, 'super', false),
    modifier(Gdk.ModifierType.MOD1_MASK, 'alt', false),
    modifier(Gdk.ModifierType.CONTROL_MASK, 'control', false),
    // this makes sure that we have at least one modifier enabled
    modifier(Gdk.ModifierType.MODIFIER_MASK, false, true)
]

module.exports = function(state, keyval) {
    const result = {
        valid: false,
        string: '',
    }

    if(state[0] && keyval[0]) {
        result.valid = true,
        result.string = Gdk.keyval_name(keyval[1])

        rules.forEach((rule) => {
            result = rule(state[1], result)
        })
    }

    return result
}

You can add a new entry to imports.searchPath : 您可以将新条目添加到imports.searchPath

$ mkdir -p ~/gjs/dynmodules/
$ touch ~/gjs/dynmodules/hello.js

imports.searchPath.push("/home/ole/gjs")
hello = imports.dynmodules.hello;

You can load the file contents into a string and eval() it to get the same effect. 您可以将文件内容加载到字符串中,然后使用eval()获得相同的效果。

Do you have a particular reason why you can't use imports to get what you want in a plugin structure? 您是否有特定的原因,为什么不能使用导入来在插件结构中获得所需的内容? You can also scan through a directory and import each JS file. 您还可以浏览目录并导入每个JS文件。 That will prevent plugins from dumping arbitrary values into your global namespace. 这样可以防止插件将任意值转储到全局名称空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Gnome / gjs / Gio中检查错误代码 - How can I check error code in Gnome/gjs/Gio 在 CJS / GJS (Gnome JavaScript) 中开发 Cinnamon Shell Extension (Desklet) 的介绍? - Introduction in developing Cinnamon Shell Extension (Desklet) in CJS / GJS (Gnome JavaScript)? 如何在JavaScript生成的输出中包含PHP文件的内容? - How can I include the content of PHP files in output generated by JavaScript? 在使用GJS进行一系列异步任务之后,我该如何安排某些事情发生? - How can I schedule something to happen after a series of asynchronous tasks with GJS? 如何通过 JavaScript 文件将所有 JavaScript 文件包含在目录中? - How can I include all JavaScript files in a directory via JavaScript file? 使用gjs,如何将Soup.Buffer数据块写入文件? - With gjs, how can I write Soup.Buffer chunks of data to a file? 如何在 vue.js 中包含第 3 方 JavaScript 文件? - How can I include 3rd party JavaScript files in vue.js? 我如何在JavaScript中包含这些节点的循环 - How can i include loop for these nodes in javascript gjs如何使用g_data_input_stream_read_line_async在Gnome Shell Extension中读取套接字流 - gjs How to read a socket stream in Gnome Shell Extension using g_data_input_stream_read_line_async 如何在书签中包含外部javascript文件? - How do I include external javascript files in bookmarklets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM