简体   繁体   English

使用javascript for phonegap复制到剪贴板

[英]Copy to Clipboard with javascript for phonegap

i am developing an app using javascript/mobile-jquery interface for the phonegap platform. 我正在使用javascript / mobile-jquery接口为phonegap平台开发一个应用程序。 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. 我对浏览器中的这个功能不感兴趣,只要它通过电话间隙转换后就能正常工作我很高兴。

I have tried using zeroclipboard, it did not workout, I have tried using clipboard manager could not get it to work. 我曾尝试使用zeroclipboard,它没有锻炼,我试过使用剪贴板管理器无法让它工作。 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: 我已经尝试了很多其他的例子,我在stackoverflow上找到了这些例子,包括google搜索,但它们仍然无法正常工作,这是我尝试过的一个例子:

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. 一个名为jquery的文件夹,里面有jquery脚本。
  2. a folder called res which has a folder called xml which has a file called plugin.xml 一个名为res的文件夹,它有一个名为xml的文件夹,它有一个名为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. 一个名为src的文件夹,它有一个名为com的文件夹,它有一个名为saatcioglu的文件夹,它有一个名为phonegap的文件夹,它有一个名为clipboardmanager的文件夹,它有一个名为ClipboardManagerPlugin.java的文件。
  4. test.html 的test.html
  5. clipboardmanager.js clipboardmanager.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? 我做错了什么?

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. 请注意,您应该检查您正在使用的PhoneGap版本以及该插件是否兼容。 Just in case you're not aware of it: not all plugins have been updated to work with PhoneGap 3.x yet. 万一你不知道它:并非所有插件都已更新,以便与PhoneGap 3.x一起使用。 To quote the readme at Github (https://build.phonegap.com/docs/plugins-using) : "Unless explicitly stated, most of these plugins will not work with Cordova/PhoneGap 3.xx out of the box. They will need updating before they can be used via the plugin add interface." 引用Github上的自述文件(https://build.phonegap.com/docs/plugins-using)“除非明确说明,否则大多数这些插件都不适用于Cordova / PhoneGap 3.xx开箱即用。他们将需要更新才能通过plugin add界面使用它们。“

e-sushi's instructions didn't quite work for me. e-sushi的说明对我来说并不适用。

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);

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

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