使用 javascript 复制到剪贴板,用于 phonegap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18361345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:37:33  来源:igfitidea点击:

Copy to Clipboard with javascript for phonegap

javascriptcordova

提问by Bojan

i am developing an app using javascript/mobile-jquery interface for the phonegap platform. Now I have seen so many examples on the web trying to copy to clipboard and unfortunatelly none of them worked for me. I am not interested in this being function-able in the browser, as long as it works once it is converted by phone gap I am happy.

我正在使用 javascript/mobile-jquery 界面为 phonegap 平台开发一个应用程序。现在我在网上看到了很多试图复制到剪贴板的例子,不幸的是没有一个对我有用。我对它在浏览器中的功能不感兴趣,只要它在通过电话间隙转换后就可以工作,我很高兴。

I have tried using zeroclipboard, it did not workout, I have tried using clipboard manager could not get it to work. I have tried many other examples that I found here on stackoverflow including google search and they still did not work, here is an example of things i've tried:

我试过使用 zeroclipboard,它没有锻炼,我试过使用剪贴板管理器无法让它工作。我已经尝试了我在 stackoverflow 上找到的许多其他示例,包括谷歌搜索,但它们仍然无法正常工作,以下是我尝试过的示例:

window.plugins.clipboardManager.copy(
                "the text to copy",
                function(r){alert("copy is successful")},
                function(e){alert(e)}
            );

I have included the js file:

我已经包含了 js 文件:

    <script src="src/clipboardmanager.js"></script>

and I also have the java file in the folder structure as this: src\com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java

我在文件夹结构中也有 java 文件,如下所示: src\com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java

From what I've read I need to include an xml file for this to work, but for the life of me I could not find that XML file anywhere.

从我读过的内容来看,我需要包含一个 xml 文件才能使其工作,但在我的一生中,我无法在任何地方找到该 XML 文件。

Any help is most appreciated.

非常感谢任何帮助。

Note: My app will require no permissions such as camera, gps, etc...

注意:我的应用不需要相机、gps 等权限...

EDIT:

编辑:

Another example I tried was:

我试过的另一个例子是:

function select_all(obj) {
    var text_val=eval(obj);
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');
}

This worked in IE but not in Phonegap.

这在 IE 中有效,但在 Phonegap 中无效。

EDIT:

编辑:

Here is the html/javascript I'm using:

这是我正在使用的 html/javascript:

<html>
    <head>
        <title>Test</title>
            <link rel="stylesheet" href="jquery/jquery.mobile-1.3.1.min.css" />
        <script src="jquery/jquery-1.9.1.min.js"></script>
        <script src="jquery/jquery.mobile-1.3.1.min.js"></script>
            <script src="clipboardmanager.js"></script>
        <script>
                var cbm = new window.plugins.clipboardManager;
                function main(textMessage)
                {
            //Some Code before this (calculations)
                    cbm.copy(
                        "Success!!!",
                        function(r){alert("copy is successful")},
                        function(e){alert(e)}
                    );
                }
            </script>
        </head>
        <body>
        <div data-role="page" id="main" name="main">
            <div data-role="header">
                <h1>Test</h1>
                </div><!-- /header -->

            <div data-role="content">
                <form action="javascript:main(encryptedMessage.value);">
                    Message to be Copied:
                    <textarea id="encryptedMessage" name="encryptedName" rows="6" style="width:99%;"></textarea>
                    <input type="submit" value="Encrypt" />
                </form>
                </div>
        </div>
    </body>
</html>

In my root folder I have:

在我的根文件夹中,我有:

  1. a folder called jquery which has jquery scripts in there.
  2. a folder called res which has a folder called xml which has a file called plugin.xml
  3. a folder called src which has a folder called com, which has a folder called saatcioglu, which has a folder called phonegap, which has a folder called clipboardmanager, which has a file called ClipboardManagerPlugin.java.
  4. test.html
  5. clipboardmanager.js
  1. 一个名为 jquery 的文件夹,其中包含 jquery 脚本。
  2. 一个名为 res 的文件夹,其中有一个名为 xml 的文件夹,其中有一个名为 plugin.xml 的文件
  3. src 文件夹,com 文件夹,saatcioglu 文件夹,phonegap 文件夹,clipboardmanager 文件夹,ClipboardManagerPlugin.java 文件。
  4. 测试.html
  5. 剪贴板管理器.js

Contents of plugin.xml

plugin.xml 的内容

<?xml version="1.0" encoding="utf-8"?>
<plugins>
    <gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />
</plugins>

What have I done wrong?

我做错了什么?

采纳答案by e-sushi

First up: that IE option will not work on Android as PhoneGap uses Webkit (think: Safari and/or Chrome).

首先:IE 选项不适用于 Android,因为 PhoneGap 使用 Webkit(想想:Safari 和/或 Chrome)。

Anyway...

反正...

That file you're looking for (in the "/res/xml/"subdirectory of your project's directory) is called

您正在寻找的文件(在项目目录的“/res/xml/”子目录中)被称为

config.xml

In there, you have to tell phonegap to load the plugin at compile time like this...

在那里,你必须告诉 phonegap 在编译时加载插件,就像这样......

<gap:plugin name="whatever" value="com.example.whatever" />

If you don't do that, phonegap will simply not include the plugin at compile time, resulting in the fact that your plugin won't work (since it doesn't exist in the compiled apk).

如果您不这样做,phonegap 将在编译时根本不包含该插件,从而导致您的插件无法工作(因为它不存在于已编译的apk 中)。

I haven't used the ClipboardManagerPlugin yet, but according to the docs it should go somewhat like this:

我还没有使用 ClipboardManagerPlugin,但根据文档,它应该是这样的:

<gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />

Please note that you should check the PhoneGap version you're using and if the plugin is compatible with it. Just in case you're not aware of it: not all plugins have been updated to work with PhoneGap 3.x yet. To quote the readme at Github (https://build.phonegap.com/docs/plugins-using): "Unless explicitlystated, most of these plugins will notwork with Cordova/PhoneGap 3.x.x out of the box. They will need updating before they can be used via the plugin addinterface."

请注意,您应该检查您使用的 PhoneGap 版本以及插件是否与其兼容。以防万一您不知道:并非所有插件都已更新为可与 PhoneGap 3.x 一起使用。引用Github (https://build.phonegap.com/docs/plugins-using) 上的自述文件:“除非明确说明,否则大多数这些插件将无法在开箱即用的 Cordova/PhoneGap 3.xx 中使用。他们将需要更新才能通过plugin add界面使用。”

回答by Marcom

e-sushi's instructionsdidn't quite work for me.

电子寿司的说明对我来说不太适用。

To get the plugin running I used the following command:

为了让插件运行,我使用了以下命令:

phonegap local plugin add https://github.com/VersoSolutions/CordovaClipboard

and then adding the following command in JS:

然后在JS中添加以下命令:

cordova.plugins.clipboard.copy(text);