Skip to content

Commit 15582ce

Browse files
committed
Added rename node
1 parent 73b9cc7 commit 15582ce

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"puppeteer-page-keyboard-press": "puppeteer/page/keyboard/press.js",
4444
"puppeteer-page-get-value": "puppeteer/page/get.js",
4545
"puppeteer-page-set-value": "puppeteer/page/set.js",
46-
"puppeteer-page-upload": "puppeteer/page/upload.js"
46+
"puppeteer-page-upload": "puppeteer/page/upload.js",
47+
"puppeteer-rename": "puppeteer/page/rename.js"
4748
}
4849
}
4950
}

puppeteer/page/icons/rename.svg

Lines changed: 6 additions & 0 deletions
Loading

puppeteer/page/rename.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType("puppeteer-rename", {
3+
category: "storage",
4+
color: "#deb887",
5+
defaults: {
6+
oldFilePath: { value: "", required: false },
7+
oldFilePathtype: { value: "str" },
8+
newFilePath: { value: "", required: false },
9+
newFilePathtype: { value: "str" },
10+
name: { value: "" },
11+
},
12+
inputs: 1,
13+
outputs: 1,
14+
icon: "rename.svg",
15+
label: function () {
16+
return name || "Rename";
17+
},
18+
paletteLabel: "Rename",
19+
oneditprepare: function () {
20+
$("#node-input-oldFilePath").typedInput({
21+
type: "str",
22+
types: ["str", "msg", "flow", "global"],
23+
typeField: "#node-input-oldFilePathtype",
24+
});
25+
$("#node-input-newFilePath").typedInput({
26+
type: "str",
27+
types: ["str", "msg", "flow", "global"],
28+
typeField: "#node-input-newFilePathtype",
29+
});
30+
},
31+
});
32+
</script>
33+
34+
<script type="text/x-red" data-template-name="puppeteer-page-rename">
35+
<div class="form-row">
36+
<label for="node-input-oldFilePath"><i class="fa fa-file"></i> Old File Path</label>
37+
<input id="node-input-oldFilePath"></input>
38+
<input type="hidden" id="node-input-oldFilePathtype"></input>
39+
</div>
40+
<div class="form-row">
41+
<label for="node-input-newFilePath"><i class="fa fa-file-o"></i> New File Path</label>
42+
<input id="node-input-newFilePath"></input>
43+
<input type="hidden" id="node-input-newFilePathtype"></input>
44+
</div>
45+
<div class="form-row">
46+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
47+
<input type="text" id="node-input-name" style="width:70%;" placeholder="Name" />
48+
</div>
49+
</script>
50+
51+
<script type="text/x-red" data-help-name="puppeteer-page-rename">
52+
<p>Click on Chrome</p>
53+
<h3>Inputs</h3>
54+
<dl class="message-properties">
55+
<dt>Selector<span class="property-type">string</span></dt>
56+
<dd>A <a href="#interface-selector" title="Selector">selector</a> to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.</dd>
57+
<dt>Button<span class="property-type">string</span></dt>
58+
<dd>Slows down Puppeteer operations by the specified amount of milliseconds. Useful so that you can see what is going on.&lt;"left"|"right"|"middle"&gt; Defaults to <code>left</code>.</dd>
59+
<dt>Click Count<span class="property-type">number</span></dt>
60+
<dd>Whether to run browser in <a href="https://developers.google.com/web/updates/2017/04/headless-chrome" rel="nofollow">headless mode</a>. Defaults to <code>false</code>, it will show Chrome when <code>devtools</code> option is <code>true</code>.defaults to 1. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail" title="UIEvent.detail" rel="nofollow">UIEvent.detail</a>.</dd>
61+
<dt>Delay<span class="property-type">number</span></dt>
62+
<dd>Specify custom debugging port. Pass <code>0</code> to discover a random port. Defaults to <code>0</code>.Time to wait between <code>mousedown</code> and <code>mouseup</code> in milliseconds. Defaults to 0.</dd>
63+
<dt>Rename path<span class="property-type">string</span></dt>
64+
<dd>Specify custom rename path. Leave the field blank for default rename path. Defaults to user's default browser rename path.</dd>
65+
</dl>
66+
<h3>Details</h3>
67+
<p>This method fetches an element with <code>selector</code>, scrolls it into view if needed, and then uses <code>page.mouse</code> to click in the center of the element.
68+
If there's no element matching <code>selector</code>, the method throws an error.</p>
69+
</script>

puppeteer/page/rename.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module.exports = function (RED) {
2+
function PuppeteerRename(nodeConfig) {
3+
RED.nodes.createNode(this, nodeConfig);
4+
var node = this; // Referencing the current node
5+
6+
this.on("input", async function (msg, send, done) {
7+
try {
8+
// Parsing the oldFilePath from string input or from msg object
9+
let oldFilePath =
10+
nodeConfig.oldFilePathtype != "str"
11+
? eval(nodeConfig.oldFilePathtype + "." + nodeConfig.oldFilePath)
12+
: nodeConfig.oldFilePath;
13+
// If the type of oldFilePath is set to flow or global, it needs to be parsed differently
14+
if (
15+
nodeConfig.oldFilePathtype == "flow" ||
16+
nodeConfig.oldFilePathtype == "global"
17+
) {
18+
// Parsing the oldFilePath
19+
oldFilePath = this.context()[nodeConfig.oldFilePathtype].get(
20+
nodeConfig.oldFilePathtype
21+
);
22+
}
23+
24+
// Parsing the newFilePath from string input or from msg object
25+
let newFilePath =
26+
nodeConfig.newFilePathtype == "msg"
27+
? eval(nodeConfig.newFilePathtype + "." + nodeConfig.newFilePath)
28+
: nodeConfig.newFilePath;
29+
// If the type of newFilePath is set to flow or global, it needs to be parsed differently
30+
if (
31+
nodeConfig.newFilePathtype == "flow" ||
32+
nodeConfig.newFilePathtype == "global"
33+
) {
34+
// Parsing the newFilePath
35+
newFilePath = this.context()[nodeConfig.newFilePathtype].get(
36+
nodeConfig.newFilePath
37+
);
38+
}
39+
40+
if(oldFilePath && newFilePath && oldFilePath != '' && newFilePath != '') {
41+
const fs = require('fs');
42+
fs.renameSync(oldFilePath, newFilePath);
43+
} else {
44+
throw 'Please enter a valid file path';
45+
}
46+
47+
// oldFilePath clicked sucessfully
48+
node.status({
49+
fill: "green",
50+
shape: "dot",
51+
text: `Clicked ${oldFilePath}`,
52+
});
53+
// Sending the msg
54+
send(msg);
55+
} catch (e) {
56+
// If an error occured
57+
node.error(e);
58+
// Update the status
59+
node.status({ fill: "red", shape: "dot", text: e });
60+
// And update the message error property
61+
msg.error = e;
62+
send(msg);
63+
}
64+
65+
// Clear status of the node
66+
setTimeout(
67+
() => {
68+
done();
69+
node.status({});
70+
},
71+
msg.error ? 10000 : 3000
72+
);
73+
});
74+
this.on("close", function () {
75+
node.status({});
76+
});
77+
oneditprepare: function oneditprepare() {
78+
$("#node-input-oldFilePath").val(nodeConfig.oldFilePath);
79+
$("#node-input-newFilePath").val(nodeConfig.newFilePath);
80+
}
81+
}
82+
RED.nodes.registerType("puppeteer-rename", PuppeteerRename);
83+
};

0 commit comments

Comments
 (0)