使用 Google Chrome 扩展程序访问本地文件 [英] Access Local Files using a Google Chrome Extension

查看:247
本文介绍了使用 Google Chrome 扩展程序访问本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前也问过类似的问题

I have asked a similar question to this previously

从网站与现金抽屉沟通

然而,这个问题非常模糊,我现在知道使用扩展程序可以满足我的需求.

However the question was very vague and I now know what I require is possible using an extension.

这个问题与以下问题类似,但不是我需要的信息.

This question is similar to the following, however, not quite the information I need.

Chrome 扩展和本地存储

通过谷歌浏览器扩展访问本地文件?

列出带有 chrome 扩展名的本地目录

我实际上需要做的是从我的网站在用户计算机上运行一个 bat 文件.该网站的用户将是公司的员工,他们将使用的计算机将由我们提供,因此我可以在向用户发货之前对机器进行任何必要的更改以使其正常工作.

What I effectively need to do is run a bat file on the users computer from my website. The users of the site will be employees of the company and the computers they will be using will be provided by us, therefore I can make any necessary changes to the machines for this to work before shipping to the user.

我了解您可以在计算机上创建一个本地文件夹,您可以允许 chrome 访问该文件夹,从而维护安全性.我已经搜索了如何执行此操作,但似乎无法确定我需要搜索的内容,尤其是这正是我需要帮助的地方.

I understand that you can create a local folder on the computer that you can allow chrome to have access to, therefore maintaining security. I have searched for how to do this but cannot seem to nail down what I need to search for and this in particular is where I need help.

http://developer.chrome.com/apps/fileSystem

我还没有构建扩展,但我愿意接受挑战,所以我不需要这方面的帮助.下面的两个指南似乎绰绰有余.

I have not yet built an extension but I am willing to take on the challenge so I do not need help with this side of things. The two guides below seem more than ample.

http://lifehacker.com/5857721/how-to-build-a-chrome-extension

http://css-tricks.com/colorpeek-part-2-building-first-chrome-extension/

这是我目前所在的位置.

This is where I am currently at.

{
  "name": "Open Till",
  "version":"1",
  "manifest_version": 2,
  "description": "Allow POS Users to Open their Till",
  "browser_action": {
    "default_icon": "icon.jpg"
  }
}

接下来我需要实现的是创建一个命令来调用扩展程序,然后扩展程序导航到 chrome 可以访问的本地文件夹,然后执行批处理文件.

What I need to achieve next is to create a command to call the extension, the extension then to navigate to the local folder that chrome can access to and then execute a batch file.

我意识到我可以通过使用 npapi 插件来解决对 chrome 文件夹的需求

I realise I could work around the need for the chrome folder by using the npapi plugin

https://developer.chrome.com/extensions/npapi

然而,这显然是一个潜在的雷区,所以我不想这样做.

However, this is apparently a potential minefield so I would not like to do this.

如果有人能指出我如何创建文件夹和访问文件的正确方向,我将不胜感激.

If someone can point me in the right direction of how to create the folder and access the file, this would be most appreciated.

推荐答案

您将需要编写一个向您的网站公开 API 的 Chrome 扩展程序.这是简单的部分.你注入 一个内容脚本到您的网站,然后使用 chrome.extension.sendMessage 与您的分机通信.

You are going to need to write a Chrome extension that exposes an API to your website. This is the easy part. You inject a content script to your website, and then use chrome.extension.sendMessage to communicate back to your extension.

困难的部分是从您的扩展中实际打开 bat 文件.使用 NPAPI,这会很容易,因为您只需编写一个调用 CreateProcess 之类的.与您所说的相反,这是一个非常好的解决方案.但不幸的是,NPAPI 不是一种选择,因为它是 已弃用.

The hard part is to actually open the bat file from your extension. With NPAPI, this would have been easy, since you could just write a C++ plugin that calls CreateProcess or something. Contrary to what you said, it's a pretty good solution. But unfortunately, NPAPI isn't an option, since it's being deprecated.

所以你应该做的是使用Native Messaging.基本上,它是一个 Chrome API,允许扩展程序使用标准输入和输出流与本机应用程序交换消息.

So what you should do is to use Native Messaging. Basically, it's a Chrome API that allows extensions to exchange messages with native applications using standard input and output streams.

在此处阅读更多相关信息:https://developer.chrome.com/extensions/messaging#native-messaging-host

Read more about it here: https://developer.chrome.com/extensions/messaging#native-messaging-host

这篇关于使用 Google Chrome 扩展程序访问本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆