-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGetWebText.jsx
More file actions
92 lines (77 loc) · 1.81 KB
/
GetWebText.jsx
File metadata and controls
92 lines (77 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// IdExtenso wants to run in INDD.
// ---
#target 'indesign'
// Path to IdExtenso entry point.
// ---
#include '../$$.jsxinc'
// Web module.
// ---
#include '../etc/$$.Web.jsxlib'
// Load the framework in TRACE mode
// ---
$$.load(-1);
// =============================================================================
// GetWebText [190323] [210203]
// Download a remote text resource.
// ---
// Demonstrates:
// - `$$.Web(url,1)`, with `wantText` option.
// =============================================================================
try
{
// URL of the resource.
// ---
var url = prompt(
__("Enter the URL of the resource:"),
"http://indiscripts.com/pages/help",
"GetWebText"
);
if( !url || !(url=url.trim()) )
{
exit(0);
}
// Check the URL.
// ---
url = $$.Web.parseURI(url);
if( !url.protocol )
{
$$.error(__("Invalid protocol in %1.", url.source) );
}
// GET the resource.
// ---
var res = $$.Web(url.source,/*wantText*/1);
if( res.error )
{
$$.error(res.error);
}
// Show what it looks like.
// ---
var sel = app.properties.selection;
var txt = res.data;
if( sel && (sel=sel[0]) && ('texts' in sel) )
{
var t = txt.replace(/^\s*\<!DOCTYPE [^\>]+\>/,'').trim();
try{ txt=XML(t).toString(); }
catch(_){ $$.trace( __("ExtendScript cannot interprete data as XML: %1.",_) ); }
sel.texts[0].contents = txt;
}
else
{
const MAX_LEN = 10000;
if( txt.length > MAX_LEN )
$$.trace( __("Data (sample): %1", txt.slice(0,MAX_LEN).toSource()) );
else
$$.trace( __("Data: %1", txt.toSource()) );
}
}
catch(e)
{
// Just in case something goes wrong.
// ---
$$.receiveError(e);
}
// =============================================================================
// Please, unload the framework to cleanup memory.
// Also, this auto-opens the log file (if active.)
// ---
$$.unload();