@capawesome/capacitor-screenshot 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CapawesomeCapacitorScreenshot.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +75 -0
  4. package/android/build.gradle +58 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/Screenshot.java +35 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/ScreenshotHelper.java +13 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/ScreenshotPlugin.java +64 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/classes/interfaces/Callback.java +5 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/classes/interfaces/NonEmptyCallback.java +7 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/classes/interfaces/Result.java +7 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/screenshot/classes/results/TakeResult.java +22 -0
  13. package/android/src/main/res/.gitkeep +0 -0
  14. package/dist/docs.json +67 -0
  15. package/dist/esm/definitions.d.ts +21 -0
  16. package/dist/esm/definitions.js +2 -0
  17. package/dist/esm/definitions.js.map +1 -0
  18. package/dist/esm/index.d.ts +4 -0
  19. package/dist/esm/index.js +7 -0
  20. package/dist/esm/index.js.map +1 -0
  21. package/dist/esm/web.d.ts +5 -0
  22. package/dist/esm/web.js +9 -0
  23. package/dist/esm/web.js.map +1 -0
  24. package/dist/plugin.cjs.js +29 -0
  25. package/dist/plugin.cjs.js.map +1 -0
  26. package/dist/plugin.js +31 -0
  27. package/dist/plugin.js.map +1 -0
  28. package/ios/Plugin/Classes/Enums/CustomError.swift +20 -0
  29. package/ios/Plugin/Classes/Protocols/Result.swift +5 -0
  30. package/ios/Plugin/Classes/Results/TakeResult.swift +15 -0
  31. package/ios/Plugin/Info.plist +24 -0
  32. package/ios/Plugin/Screenshot.swift +45 -0
  33. package/ios/Plugin/ScreenshotPlugin.h +10 -0
  34. package/ios/Plugin/ScreenshotPlugin.m +8 -0
  35. package/ios/Plugin/ScreenshotPlugin.swift +37 -0
  36. package/package.json +98 -0
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'CapawesomeCapacitorScreenshot'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.license = package['license']
10
+ s.homepage = package['repository']['url']
11
+ s.author = package['author']
12
+ s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
+ s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
14
+ s.ios.deployment_target = '13.0'
15
+ s.dependency 'Capacitor'
16
+ s.swift_version = '5.1'
17
+ end
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Robin Genz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # @capawesome/capacitor-screenshot
2
+
3
+ Capacitor plugin for taking screenshots.
4
+
5
+ ## Installation
6
+
7
+ Install the plugin:
8
+
9
+ ```bash
10
+ npm install @capawesome/capacitor-screenshot
11
+ npx cap sync
12
+ ```
13
+
14
+ If you are using the Web platform, you must also install the `html2canvas` package:
15
+
16
+ ```bash
17
+ npm i html2canvas
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { Screenshot } from '@capawesome/capacitor-screenshot';
24
+
25
+ const take = async () => {
26
+ const { uri } = await Screenshot.take();
27
+ console.log('Screenshot saved at:', uri);
28
+ };
29
+ ```
30
+
31
+ ## API
32
+
33
+ <docgen-index>
34
+
35
+ * [`take()`](#take)
36
+ * [Interfaces](#interfaces)
37
+
38
+ </docgen-index>
39
+
40
+ <docgen-api>
41
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
42
+
43
+ ### take()
44
+
45
+ ```typescript
46
+ take() => Promise<TakeResult>
47
+ ```
48
+
49
+ Take a screenshot.
50
+
51
+ **Returns:** <code>Promise&lt;<a href="#takeresult">TakeResult</a>&gt;</code>
52
+
53
+ **Since:** 6.0.0
54
+
55
+ --------------------
56
+
57
+
58
+ ### Interfaces
59
+
60
+
61
+ #### TakeResult
62
+
63
+ | Prop | Type | Description | Since |
64
+ | --------- | ------------------- | -------------------------------------------------------------------- | ----- |
65
+ | **`uri`** | <code>string</code> | The file path (Android and iOS) or data URI (Web) of the screenshot. | 6.0.0 |
66
+
67
+ </docgen-api>
68
+
69
+ ## Changelog
70
+
71
+ See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/screenshot/CHANGELOG.md).
72
+
73
+ ## License
74
+
75
+ See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/screenshot/LICENSE).
@@ -0,0 +1,58 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
6
+ }
7
+
8
+ buildscript {
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+ dependencies {
14
+ classpath 'com.android.tools.build:gradle:8.2.1'
15
+ }
16
+ }
17
+
18
+ apply plugin: 'com.android.library'
19
+
20
+ android {
21
+ namespace "io.capawesome.capacitorjs.plugins.screenshot"
22
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34
23
+ defaultConfig {
24
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
25
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34
26
+ versionCode 1
27
+ versionName "1.0"
28
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29
+ }
30
+ buildTypes {
31
+ release {
32
+ minifyEnabled false
33
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34
+ }
35
+ }
36
+ lintOptions {
37
+ abortOnError false
38
+ }
39
+ compileOptions {
40
+ sourceCompatibility JavaVersion.VERSION_17
41
+ targetCompatibility JavaVersion.VERSION_17
42
+ }
43
+ }
44
+
45
+ repositories {
46
+ google()
47
+ mavenCentral()
48
+ }
49
+
50
+
51
+ dependencies {
52
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
53
+ implementation project(':capacitor-android')
54
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
55
+ testImplementation "junit:junit:$junitVersion"
56
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
57
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
58
+ }
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,35 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot;
2
+
3
+ import android.content.Context;
4
+ import android.graphics.Bitmap;
5
+ import android.webkit.WebView;
6
+ import androidx.annotation.NonNull;
7
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces.NonEmptyCallback;
8
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces.Result;
9
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.results.TakeResult;
10
+ import java.io.File;
11
+ import java.io.FileOutputStream;
12
+
13
+ public class Screenshot {
14
+
15
+ private final ScreenshotPlugin plugin;
16
+
17
+ public Screenshot(@NonNull ScreenshotPlugin plugin) {
18
+ this.plugin = plugin;
19
+ }
20
+
21
+ public void take(@NonNull NonEmptyCallback<Result> callback) {
22
+ WebView webView = this.plugin.getBridge().getWebView();
23
+ Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);
24
+ webView.draw(new android.graphics.Canvas(bitmap));
25
+ File screenshot = ScreenshotHelper.createFileOnCache(this.plugin.getContext());
26
+ try (FileOutputStream outputStream = new FileOutputStream(screenshot)) {
27
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
28
+ outputStream.flush();
29
+ TakeResult result = new TakeResult(screenshot);
30
+ callback.success(result);
31
+ } catch (Exception error) {
32
+ callback.error(error);
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,13 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot;
2
+
3
+ import android.content.Context;
4
+ import java.io.File;
5
+ import java.util.UUID;
6
+
7
+ public class ScreenshotHelper {
8
+
9
+ static File createFileOnCache(Context context) {
10
+ String fileName = UUID.randomUUID().toString() + ".jpg";
11
+ return new File(context.getCacheDir(), fileName);
12
+ }
13
+ }
@@ -0,0 +1,64 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.JSObject;
6
+ import com.getcapacitor.Logger;
7
+ import com.getcapacitor.Plugin;
8
+ import com.getcapacitor.PluginCall;
9
+ import com.getcapacitor.PluginMethod;
10
+ import com.getcapacitor.annotation.CapacitorPlugin;
11
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces.NonEmptyCallback;
12
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces.Result;
13
+
14
+ @CapacitorPlugin(name = "Screenshot")
15
+ public class ScreenshotPlugin extends Plugin {
16
+
17
+ public static final String ERROR_UNKNOWN_ERROR = "An unknown error has occurred.";
18
+ public static final String TAG = "ScreenshotPlugin";
19
+
20
+ private Screenshot implementation;
21
+
22
+ @Override
23
+ public void load() {
24
+ super.load();
25
+ this.implementation = new Screenshot(this);
26
+ }
27
+
28
+ @PluginMethod
29
+ public void take(PluginCall call) {
30
+ try {
31
+ NonEmptyCallback<Result> callback = new NonEmptyCallback<>() {
32
+ @Override
33
+ public void success(@NonNull Result result) {
34
+ resolveCall(call, result.toJSObject());
35
+ }
36
+
37
+ @Override
38
+ public void error(Exception exception) {
39
+ rejectCall(call, exception);
40
+ }
41
+ };
42
+ implementation.take(callback);
43
+ } catch (Exception exception) {
44
+ rejectCall(call, exception);
45
+ }
46
+ }
47
+
48
+ private void resolveCall(@NonNull PluginCall call, @Nullable JSObject result) {
49
+ if (result == null) {
50
+ call.resolve();
51
+ } else {
52
+ call.resolve(result);
53
+ }
54
+ }
55
+
56
+ private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
57
+ String message = exception.getMessage();
58
+ if (message == null) {
59
+ message = ERROR_UNKNOWN_ERROR;
60
+ }
61
+ Logger.error(TAG, message, exception);
62
+ call.reject(message);
63
+ }
64
+ }
@@ -0,0 +1,5 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces;
2
+
3
+ public interface Callback {
4
+ void error(Exception exception);
5
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ public interface NonEmptyCallback<T> extends Callback {
6
+ void success(@NonNull T result);
7
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces;
2
+
3
+ import com.getcapacitor.JSObject;
4
+
5
+ public interface Result {
6
+ JSObject toJSObject();
7
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.screenshot.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.screenshot.classes.interfaces.Result;
6
+ import java.io.File;
7
+
8
+ public class TakeResult implements Result {
9
+
10
+ private final String path;
11
+
12
+ public TakeResult(@NonNull File file) {
13
+ path = file.getAbsolutePath();
14
+ }
15
+
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("uri", path);
20
+ return result;
21
+ }
22
+ }
File without changes
package/dist/docs.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "api": {
3
+ "name": "ScreenshotPlugin",
4
+ "slug": "screenshotplugin",
5
+ "docs": "",
6
+ "tags": [],
7
+ "methods": [
8
+ {
9
+ "name": "take",
10
+ "signature": "() => Promise<TakeResult>",
11
+ "parameters": [],
12
+ "returns": "Promise<TakeResult>",
13
+ "tags": [
14
+ {
15
+ "name": "since",
16
+ "text": "6.0.0"
17
+ }
18
+ ],
19
+ "docs": "Take a screenshot.",
20
+ "complexTypes": [
21
+ "TakeResult"
22
+ ],
23
+ "slug": "take"
24
+ }
25
+ ],
26
+ "properties": []
27
+ },
28
+ "interfaces": [
29
+ {
30
+ "name": "TakeResult",
31
+ "slug": "takeresult",
32
+ "docs": "",
33
+ "tags": [
34
+ {
35
+ "text": "6.0.0",
36
+ "name": "since"
37
+ }
38
+ ],
39
+ "methods": [],
40
+ "properties": [
41
+ {
42
+ "name": "uri",
43
+ "tags": [
44
+ {
45
+ "text": "6.0.0",
46
+ "name": "since"
47
+ },
48
+ {
49
+ "text": "'content://com.android.providers.downloads.documents/document/msf%3A1000000073'",
50
+ "name": "example"
51
+ },
52
+ {
53
+ "text": "'data:image/jpeg;base64,SGVsbG8sIFdvc...mxkIQ=='",
54
+ "name": "example"
55
+ }
56
+ ],
57
+ "docs": "The file path (Android and iOS) or data URI (Web) of the screenshot.",
58
+ "complexTypes": [],
59
+ "type": "string"
60
+ }
61
+ ]
62
+ }
63
+ ],
64
+ "enums": [],
65
+ "typeAliases": [],
66
+ "pluginConfigs": []
67
+ }
@@ -0,0 +1,21 @@
1
+ export interface ScreenshotPlugin {
2
+ /**
3
+ * Take a screenshot.
4
+ *
5
+ * @since 6.0.0
6
+ */
7
+ take(): Promise<TakeResult>;
8
+ }
9
+ /**
10
+ * @since 6.0.0
11
+ */
12
+ export interface TakeResult {
13
+ /**
14
+ * The file path (Android and iOS) or data URI (Web) of the screenshot.
15
+ *
16
+ * @since 6.0.0
17
+ * @example 'content://com.android.providers.downloads.documents/document/msf%3A1000000073'
18
+ * @example 'data:image/jpeg;base64,SGVsbG8sIFdvc...mxkIQ=='
19
+ */
20
+ uri: string;
21
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["export interface ScreenshotPlugin {\n /**\n * Take a screenshot.\n *\n * @since 6.0.0\n */\n take(): Promise<TakeResult>;\n}\n\n/**\n * @since 6.0.0\n */\nexport interface TakeResult {\n /**\n * The file path (Android and iOS) or data URI (Web) of the screenshot.\n *\n * @since 6.0.0\n * @example 'content://com.android.providers.downloads.documents/document/msf%3A1000000073'\n * @example 'data:image/jpeg;base64,SGVsbG8sIFdvc...mxkIQ=='\n */\n uri: string;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { ScreenshotPlugin } from './definitions';
2
+ declare const Screenshot: ScreenshotPlugin;
3
+ export * from './definitions';
4
+ export { Screenshot };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const Screenshot = registerPlugin('Screenshot', {
3
+ web: () => import('./web').then(m => new m.ScreenshotWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { Screenshot };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,UAAU,GAAG,cAAc,CAAmB,YAAY,EAAE;IAChE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;CAC5D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { ScreenshotPlugin } from './definitions';\n\nconst Screenshot = registerPlugin<ScreenshotPlugin>('Screenshot', {\n web: () => import('./web').then(m => new m.ScreenshotWeb()),\n});\n\nexport * from './definitions';\nexport { Screenshot };\n"]}
@@ -0,0 +1,5 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { ScreenshotPlugin, TakeResult } from './definitions';
3
+ export declare class ScreenshotWeb extends WebPlugin implements ScreenshotPlugin {
4
+ take(): Promise<TakeResult>;
5
+ }
@@ -0,0 +1,9 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import html2canvas from 'html2canvas';
3
+ export class ScreenshotWeb extends WebPlugin {
4
+ async take() {
5
+ const canvas = await html2canvas(document.body);
6
+ return { uri: canvas.toDataURL() };
7
+ }
8
+ }
9
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,WAAW,MAAM,aAAa,CAAC;AAItC,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC1C,KAAK,CAAC,IAAI;QACR,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAChD,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;IACrC,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\nimport html2canvas from 'html2canvas';\n\nimport type { ScreenshotPlugin, TakeResult } from './definitions';\n\nexport class ScreenshotWeb extends WebPlugin implements ScreenshotPlugin {\n async take(): Promise<TakeResult> {\n const canvas = await html2canvas(document.body);\n return { uri: canvas.toDataURL() };\n }\n}\n"]}
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@capacitor/core');
6
+ var html2canvas = require('html2canvas');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var html2canvas__default = /*#__PURE__*/_interopDefaultLegacy(html2canvas);
11
+
12
+ const Screenshot = core.registerPlugin('Screenshot', {
13
+ web: () => Promise.resolve().then(function () { return web; }).then(m => new m.ScreenshotWeb()),
14
+ });
15
+
16
+ class ScreenshotWeb extends core.WebPlugin {
17
+ async take() {
18
+ const canvas = await html2canvas__default["default"](document.body);
19
+ return { uri: canvas.toDataURL() };
20
+ }
21
+ }
22
+
23
+ var web = /*#__PURE__*/Object.freeze({
24
+ __proto__: null,
25
+ ScreenshotWeb: ScreenshotWeb
26
+ });
27
+
28
+ exports.Screenshot = Screenshot;
29
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Screenshot = registerPlugin('Screenshot', {\n web: () => import('./web').then(m => new m.ScreenshotWeb()),\n});\nexport * from './definitions';\nexport { Screenshot };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nimport html2canvas from 'html2canvas';\nexport class ScreenshotWeb extends WebPlugin {\n async take() {\n const canvas = await html2canvas(document.body);\n return { uri: canvas.toDataURL() };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin","html2canvas"],"mappings":";;;;;;;;;;;AACK,MAAC,UAAU,GAAGA,mBAAc,CAAC,YAAY,EAAE;AAChD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;AAC/D,CAAC;;ACDM,MAAM,aAAa,SAASC,cAAS,CAAC;AAC7C,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,MAAM,MAAM,GAAG,MAAMC,+BAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AACxD,QAAQ,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;AAC3C,KAAK;AACL;;;;;;;;;"}
package/dist/plugin.js ADDED
@@ -0,0 +1,31 @@
1
+ var capacitorScreenshot = (function (exports, core, html2canvas) {
2
+ 'use strict';
3
+
4
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
5
+
6
+ var html2canvas__default = /*#__PURE__*/_interopDefaultLegacy(html2canvas);
7
+
8
+ const Screenshot = core.registerPlugin('Screenshot', {
9
+ web: () => Promise.resolve().then(function () { return web; }).then(m => new m.ScreenshotWeb()),
10
+ });
11
+
12
+ class ScreenshotWeb extends core.WebPlugin {
13
+ async take() {
14
+ const canvas = await html2canvas__default["default"](document.body);
15
+ return { uri: canvas.toDataURL() };
16
+ }
17
+ }
18
+
19
+ var web = /*#__PURE__*/Object.freeze({
20
+ __proto__: null,
21
+ ScreenshotWeb: ScreenshotWeb
22
+ });
23
+
24
+ exports.Screenshot = Screenshot;
25
+
26
+ Object.defineProperty(exports, '__esModule', { value: true });
27
+
28
+ return exports;
29
+
30
+ })({}, capacitorExports, html2canvas);
31
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Screenshot = registerPlugin('Screenshot', {\n web: () => import('./web').then(m => new m.ScreenshotWeb()),\n});\nexport * from './definitions';\nexport { Screenshot };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nimport html2canvas from 'html2canvas';\nexport class ScreenshotWeb extends WebPlugin {\n async take() {\n const canvas = await html2canvas(document.body);\n return { uri: canvas.toDataURL() };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin","html2canvas"],"mappings":";;;;;;;AACK,UAAC,UAAU,GAAGA,mBAAc,CAAC,YAAY,EAAE;IAChD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;IAC/D,CAAC;;ICDM,MAAM,aAAa,SAASC,cAAS,CAAC;IAC7C,IAAI,MAAM,IAAI,GAAG;IACjB,QAAQ,MAAM,MAAM,GAAG,MAAMC,+BAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACxD,QAAQ,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;IAC3C,KAAK;IACL;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,20 @@
1
+ import Foundation
2
+
3
+ public enum CustomError: Error {
4
+ case webviewUnavailable
5
+ case imageMissing
6
+ case implementationUnavailable
7
+ }
8
+
9
+ extension CustomError: LocalizedError {
10
+ public var errorDescription: String? {
11
+ switch self {
12
+ case .webviewUnavailable:
13
+ return NSLocalizedString("webview is not available.", comment: "webviewUnAvailable")
14
+ case .imageMissing:
15
+ return NSLocalizedString("image is missing.", comment: "imageMissing")
16
+ case .implementationUnavailable:
17
+ return NSLocalizedString("implementation is not available.", comment: "implementationUnavailable")
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ import Foundation
2
+
3
+ @objc public protocol Result {
4
+ @objc func toJSObject() -> AnyObject
5
+ }
@@ -0,0 +1,15 @@
1
+ import Capacitor
2
+
3
+ @objc public class TakeResult: NSObject, Result {
4
+ let uri: String
5
+
6
+ init(uri: String) {
7
+ self.uri = uri
8
+ }
9
+
10
+ public func toJSObject() -> AnyObject {
11
+ var result = JSObject()
12
+ result["uri"] = uri
13
+ return result as AnyObject
14
+ }
15
+ }
@@ -0,0 +1,24 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>CFBundleDevelopmentRegion</key>
6
+ <string>$(DEVELOPMENT_LANGUAGE)</string>
7
+ <key>CFBundleExecutable</key>
8
+ <string>$(EXECUTABLE_NAME)</string>
9
+ <key>CFBundleIdentifier</key>
10
+ <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11
+ <key>CFBundleInfoDictionaryVersion</key>
12
+ <string>6.0</string>
13
+ <key>CFBundleName</key>
14
+ <string>$(PRODUCT_NAME)</string>
15
+ <key>CFBundlePackageType</key>
16
+ <string>FMWK</string>
17
+ <key>CFBundleShortVersionString</key>
18
+ <string>1.0</string>
19
+ <key>CFBundleVersion</key>
20
+ <string>$(CURRENT_PROJECT_VERSION)</string>
21
+ <key>NSPrincipalClass</key>
22
+ <string></string>
23
+ </dict>
24
+ </plist>
@@ -0,0 +1,45 @@
1
+ import Foundation
2
+ import WebKit
3
+
4
+ @objc public class Screenshot: NSObject {
5
+ private let plugin: ScreenshotPlugin
6
+
7
+ public init(plugin: ScreenshotPlugin) {
8
+ self.plugin = plugin
9
+ }
10
+
11
+ @objc public func take(completion: @escaping (Result?, Error?) -> Void) {
12
+ guard let webView = plugin.bridge?.webView else {
13
+ completion(nil, CustomError.webviewUnavailable)
14
+ return
15
+ }
16
+
17
+ DispatchQueue.main.async {
18
+ let snapshotConfiguration = WKSnapshotConfiguration()
19
+ snapshotConfiguration.rect = webView.bounds
20
+ webView.takeSnapshot(with: snapshotConfiguration) { image, error in
21
+ if let error = error {
22
+ completion(nil, error)
23
+ }
24
+
25
+ if let image, let imageData = image.jpegData(compressionQuality: 1.0) {
26
+ let filename = UUID().uuidString + ".jpg"
27
+ guard let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
28
+ completion(nil, CustomError.imageMissing)
29
+ return
30
+ }
31
+ let fileURL = cacheDirectory.appendingPathComponent(filename)
32
+ do {
33
+ try imageData.write(to: fileURL)
34
+ let result = TakeResult(uri: fileURL.absoluteString)
35
+ completion(result, nil)
36
+ } catch let error {
37
+ completion(nil, error)
38
+ }
39
+ } else {
40
+ completion(nil, CustomError.imageMissing)
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,10 @@
1
+ #import <UIKit/UIKit.h>
2
+
3
+ //! Project version number for Plugin.
4
+ FOUNDATION_EXPORT double PluginVersionNumber;
5
+
6
+ //! Project version string for Plugin.
7
+ FOUNDATION_EXPORT const unsigned char PluginVersionString[];
8
+
9
+ // In this header, you should import all the public headers of your framework using statements like #import <Plugin/PublicHeader.h>
10
+
@@ -0,0 +1,8 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <Capacitor/Capacitor.h>
3
+
4
+ // Define the plugin using the CAP_PLUGIN Macro, and
5
+ // each method the plugin supports using the CAP_PLUGIN_METHOD macro.
6
+ CAP_PLUGIN(ScreenshotPlugin, "Screenshot",
7
+ CAP_PLUGIN_METHOD(take, CAPPluginReturnPromise);
8
+ )
@@ -0,0 +1,37 @@
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ /**
5
+ * Please read the Capacitor iOS Plugin Development Guide
6
+ * here: https://capacitorjs.com/docs/plugins/ios
7
+ */
8
+ @objc(ScreenshotPlugin)
9
+ public class ScreenshotPlugin: CAPPlugin {
10
+ public let tag = "Screenshot"
11
+ private var implementation: Screenshot?
12
+
13
+ override public func load() {
14
+ super.load()
15
+ self.implementation = Screenshot(plugin: self)
16
+ }
17
+
18
+ @objc func take(_ call: CAPPluginCall) {
19
+ implementation?.take(completion: { result, error in
20
+ if let error = error {
21
+ self.rejectCall(call, error)
22
+ }
23
+ if let result = result?.toJSObject() as? JSObject {
24
+ self.resolveCall(call, result)
25
+ }
26
+ })
27
+ }
28
+
29
+ private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
30
+ CAPLog.print("[", self.tag, "] ", error)
31
+ call.reject(error.localizedDescription)
32
+ }
33
+
34
+ private func resolveCall(_ call: CAPPluginCall, _ result: JSObject) {
35
+ call.resolve(result)
36
+ }
37
+ }
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "@capawesome/capacitor-screenshot",
3
+ "version": "6.0.0",
4
+ "description": "Capacitor plugin for taking screenshots.",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Plugin/",
14
+ "CapawesomeCapacitorScreenshot.podspec"
15
+ ],
16
+ "author": "Robin Genz <mail@robingenz.dev>",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/capawesome-team/capacitor-plugins.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/capawesome-team/capacitor-plugins/issues"
24
+ },
25
+ "funding": [
26
+ {
27
+ "type": "github",
28
+ "url": "https://github.com/sponsors/capawesome-team/"
29
+ },
30
+ {
31
+ "type": "opencollective",
32
+ "url": "https://opencollective.com/capawesome"
33
+ }
34
+ ],
35
+ "homepage": "https://capawesome.io/plugins/screenshot/",
36
+ "keywords": [
37
+ "capacitor",
38
+ "plugin",
39
+ "native",
40
+ "ionic",
41
+ "android",
42
+ "ios",
43
+ "screenshot"
44
+ ],
45
+ "scripts": {
46
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
47
+ "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
48
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
49
+ "verify:web": "npm run build",
50
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
51
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
52
+ "eslint": "eslint . --ext ts",
53
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
54
+ "swiftlint": "node-swiftlint",
55
+ "docgen": "docgen --api ScreenshotPlugin --output-readme README.md --output-json dist/docs.json",
56
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js",
57
+ "clean": "rimraf ./dist",
58
+ "watch": "tsc --watch",
59
+ "ios:pod:install": "cd ios && pod install --repo-update && cd ..",
60
+ "prepublishOnly": "npm run build"
61
+ },
62
+ "devDependencies": {
63
+ "@capacitor/android": "6.0.0",
64
+ "@capacitor/cli": "6.0.0",
65
+ "@capacitor/core": "6.0.0",
66
+ "@capacitor/docgen": "0.2.0",
67
+ "@capacitor/ios": "6.0.0",
68
+ "@ionic/eslint-config": "0.3.0",
69
+ "@ionic/swiftlint-config": "1.1.2",
70
+ "eslint": "7.32.0",
71
+ "html2canvas": "1.4.1",
72
+ "prettier": "2.3.2",
73
+ "prettier-plugin-java": "1.0.2",
74
+ "rimraf": "3.0.2",
75
+ "rollup": "2.77.2",
76
+ "swiftlint": "1.0.1",
77
+ "typescript": "4.1.5"
78
+ },
79
+ "peerDependencies": {
80
+ "@capacitor/core": "^6.0.0",
81
+ "html2canvas": "^1.4.1"
82
+ },
83
+ "swiftlint": "@ionic/swiftlint-config",
84
+ "eslintConfig": {
85
+ "extends": "@ionic/eslint-config/recommended"
86
+ },
87
+ "capacitor": {
88
+ "ios": {
89
+ "src": "ios"
90
+ },
91
+ "android": {
92
+ "src": "android"
93
+ }
94
+ },
95
+ "publishConfig": {
96
+ "access": "public"
97
+ }
98
+ }