Get async variable return empty - javascript

I am trying to create an add-in for the outlook windows application.
The following code should copy the email body to the clipboard, but it doesn't do that.
I need to press the run button two times in order to get the content copied, but I need to copy the content from the first time!
What is wrong with my code?
var messageBody = "";
export async function run() {
Office.context.mailbox.item.body.getAsync(
Office.CoercionType.Text,
function (asyncResult) {
if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
messageBody = asyncResult.error;
} else {
messageBody = asyncResult.value;
}
});
copyToClipboard(messageBody)
}
function copyToClipboard(text) {
var copyhelper = document.createElement("input");
copyhelper.className = 'copyhelper'
document.body.appendChild(copyhelper);
copyhelper.value = text;
copyhelper.select();
document.execCommand("copy");
document.body.removeChild(copyhelper);
}

Related

Chrome won't load simple JavaScript

My JavaScript inside the head tag:
<script type="text/javascript">
function over1() {
var img1 = document.getElementById("1").src;
document.getElementById("big").src = img1;
}
function out() {
document.getElementById("big").src = "http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/shop-icon.png";
}
function london() {
var city = document.getElementById("city").value;
var check = city.toLowerCase();
var province = document.getElementById("province").value;
if (check == "london" && province == "ON") {
alert("Visit our company travel store at Masonville Mall!");
}
}
function  checkinput()  {
var email = document.contest.email.value;
var emailcheck = email.search("#");
if (!document.contest.name.value)  {
alert("Enter a name!")
} else {
alert("Thank You " + document.contest.name.value + " " + document.contest.lastname.value + " For Entering The Contest!")
window.open(calculator.html,'_blank');
}
}
</script>
I have the simple JavaScript inside the HTML file, but Chrome won't read it. In Inspector View, it throws ReferenceErrors for all my functions. Please help.
Why do you say that, those are all functions, nothing is invoked from these functions. call the functions and see if they are invoked correctly or not
checkinput();
over1();
/* the rest of them */

how to get js context from htmlparser2 directly?

how to get js context from htmlparser2 directly?
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
},
onclosetag: function(tagname){
if(tagname === "script"){
//console.log("That's it?!");
}
}
}, {decodeEntities: true});
var input_src = fs.readFileSync('../input/test2.js', 'utf-8');
parser.write(input_src);
parser.end();
I try to get js from htmlparser2, but the content is not i am want.The content will include some other information expect the js. why the code do not work well?Could you give me some good ways to solve it?
You are not doing anything to limit the ontext function to only reading scripts. You need to set a flag whenever you encounter an opening script tag:
var inScriptElement = false;
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
inScriptElement = true;
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
if (inScriptElement) {
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
}
},
onclosetag: function(tagname){
if(tagname === "script"){
inScriptElement = false;
//console.log("That's it?!");
}
}
}, {decodeEntities: true});

How to get JS variable from Google Script?

Hello guys im new on Google Script,
im making an app that read the input from the box,
and then send it to the mysql.
I have this:
-
Codigo.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('getIp');
}
var connection = Jdbc.getConnection("jdbc:mysql://HOST:PORT/DB", "USER", "PW");
// perform the query
var SQLstatement = connection.createStatement();
var result = SQLstatement.executeQuery("Insert Into IPS Values(IPFromPrompt,0)");
Look at "IPFromPrompt" there is where i want IP from the following code
GetIp.html
<div>
<p>Click the button and enter your IP Addess.</p>
<button onclick="myFunction()">Try it</button>
<p id="AddIP"></p>
<script>
function myFunction() {
var IP = prompt("Please enter your IP Address", "");
if (person != null) {
document.getElementById("AddIP").innerHTML =
"Added " + IP + "to our database";
}
}
</script>
</div>
Do you know how can i do that?
I think you've got multiple problems here. You need a google.script.run call to trigger the .gs code from the HTML. Your myFunction() function needs to have google.script.run.
<script>
function myFunction() {
var IP = prompt("Please enter your IP Address", "");
var theUserInput = To Do . . .Get user input;
google.script.run
.withSuccessHandler(onSuccess)
.processInput(theUserInput)
}
function onSuccess(argIP) {
if (person != null) {
document.getElementById("AddIP").innerHTML =
"Added " + argIP + "to our database";
}
};
</script>
Codigo.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('getIp');
}
function processInput(argGetInput) {
var connection = Jdbc.getConnection("jdbc:mysql://HOST:PORT/DB", "USER", "PW");
// perform the query
var SQLstatement = connection.createStatement();
var result = SQLstatement.executeQuery("Insert Into IPS Values(IPFromPrompt,0)");
return result;
};

How to deliver value from popup.js to background.js

I hope to deliver the value from popup.js to background.js so that I can open the website as what i expect. I use the localStorage variable as my json's value. But when found that the value I have delieverd to background.js in the argument input of the function openTab(input) is always the string "localStorage.input" itself. How can I solve it?
popup.js
window.onload=function()
{
localStorage.input=document.getElementById("search").value;
document.getElementById("submit").onclick=function()
{
chrome.extension.sendMessage({command:"start",input:localStorage.input});
}
}
background.js
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse)
{
switch(request.command)
{
case "start":
openTab(request.input);
break;
}
return true;
}
);
var openTab=function(input)
{
chrome.windows.create
(
{
url:"http://www.baidu.com/s?wd="+input,
}
);
};
try this out
var lStore = localStorage.input || '';
window.onload=function()
{
var search = document.getElementById("search");
search.value = lStore
document.getElementById("submit").onclick=function()
{
// var input = search.value; //try this as well
var input = lStore;
chrome.extension.sendMessage({command:"start",input:input});
}
}

How to send a post request for file input in javascript?

I wrote this program to help me send post request by javascript
only the elements with send class will be sent
I store all data in a variable, because this can be re-used in another ajax function.
and then use it to create form and send it out.
this can send any data in the page, not just form input element
but it cannot send file, because I don't know how to store file input element to variable, and then create input file element for that file.
Is it possible to make my program to handle file input?
function send(url)
{
var data=getData();
var form=document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', url);
for(x in data)
{
var hidden=document.createElement('input');
hidden.setAttribute('type', 'hidden');
hidden.setAttribute('name', x);
hidden.setAttribute('value', data[x]);
form.appendChild(hidden);
}
document.body.appendChild(form);
form.submit();
}
function getData()
{
var data={};
var sendNode = document.getElementsByClassName('send');
for(var x=0; x<sendNode.length; x++)
{
var node=sendNode[x];
if(node.nodeName=='INPUT')
{
var nodeType=node.type;
if(nodeType=='check')
{
if(data[node.getAttribute('name')])
{
data[node.getAttribute('name')].push(node.value);
}
else
{
var arr=[node.value];
data[node.getAttribute('name')] = arr;
}
}
else if(nodeType=='radio')
{
if(node.checked)
{
data[node.getAttribute('name')] = node.value;
}
}
else //text, password, email
{
data[node.getAttribute('name')] = node.value;
}
}
else if(node.nodeName=='SELECT' || node.nodeName=='TEXTAREA')
{
data[node.getAttribute('name')] = node.value;
}
else
{
data[node.getAttribute('data-name')] = node.innerHTML;
}
}
return data;
}

Categories

Resources