Click a _self targeted link twice in GAS? - google-apps-script

First of all I am sorry if I use GAS tools incorrectly.
Context :
I need to generate a simple webapp including link that will be framed in a google-site.
target="_self" and .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) are ensential for this reason. I cannot set target on any other values than _self, (I'm aware that the following code wok perfectly with _top)
see this previous question : Can't have link in Embedded web app in Google site?
Code :
Use this simple code sample deployed as an Web app :
function doGet(e) {
var count = Number(e.parameters.count) || 0
var html = "<a target='_self' href='https://script.google.com/macros/s/";
html += ScriptApp.getService().getUrl().substr(35); //the id of the script
html += "?count="+ (count+1) + "'>click me twice</a><br>" + count;
return HtmlService.createHtmlOutput(html).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
It just insert a link to the curent page, counting in the get how many times the link is clicked.
Issues :
First you can see that the code dont work in the "latest code" preview (url ending by /dev). setXFrameOptionsMode dont seem to work on preview. We cant test it their but we can still deploy it so that's ok.
Once published (url ending by /exec) the web app seem to work correctly at first sight but you can click on the link only once.

Related

Google apps script - UrlFetchApp to get image blob gets timeout error

I have created a script to automatically send a subscription-confirmation e-mail when a client submits a form.
In the e-mail, I embed an image that is fetched from my website with the UrlFetchApp class (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app).
var logoUrl = "https://my.site.com/path/to/logo.png"
var logoBlob = UrlFetchApp.fetch(logoUrl).getBlob().setName("logo");
...
MailApp.sendEmail({
to: client.email,
subject: "...",
htmlBody: // html with reference to image like this:
"<img src='cid:logo' width='107' height='160'/>"
,
inlineImages: {
logo: logoBlob,
}
});
Since last week, this UrlFetchApp line causes a time-out error (error message in Dutch):
Time-out: https://my.site.com/path/to/logo.png (regel 4, bestand 'inschrijvingsbevestiging', project 'inschrijvingsbevestiging')
This has worked before without any problem, the image file also is accessible from the given URL with a browser.
I'm new to javascript and google-apps-scripts so I have no idea where to start searching for a solution. The reference manual does not mention the time-out.
Any ideas on how to solve this?
EDIT When I change the logoUrl path to some other image (I tried "https://www.google.be/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", the script works fine. So maybe there is a problem with my site?
EDIT 2 Since the script works fine with another logo, the problem must lie on my webhost. I worked around the problem by putting the logo on my Drive, as suggested in the comments.

Editing HTML content in Google Sites

Editing HTML content in Google Sites
I have a small project in mind but cant seem to get past this one obstacle.
Having trouble getting the HTML content of this Google Sites Page and adding a single line too it. So if one would keep refreshing the page the 'Hello World' line would keep adding itself over and over. I have noticed that the original code that embeds the gadget gets altered quite a bit too.
function doGet() {
var app = UiApp.createApplication();
var page = SitesApp.getActivePage();
var html = page.getHtmlContent();
var main = page.setHtmlContent(html + '<br>Hello World<br>');
return app;
}
.getHtmlContent() will return all the html markup and tags. So, you're trying to set "Hello World" after all the tags have been closed.
Throw a Logger.log(html); in there to see what you need to get at and change. Ultimately you'll need to replace the text you want within the html instead of just adding a line to the end.
its by design that the html keeps chsnging every time you run it. your script permanently modifies the site.
instead you need to publish a htnlservice appscript and insert it on the site.

How can you add a link to a google site via a Google Apps Script?

I have been scripting a few ways to make my Google site easier to manage by dynamically creating pages. I want to be able to create a directory with links to those pages.
So i was thinking something like...
var page = site.getChildByName("NEW URL");
var link = [page.getUrl()];
page = site.getChildByName("URL TO DIRECTORY").addListItem(link);
...using google apps scripts. Haven't tested this too thoroughly as of yet but it haven't been able to get the links to work in the list. I was hoping to just keep the page template as a normal webpage.
If I went that route would editing the html be the only option? Hopefully there is an easier way.
Any ideas would be great. Thanks!
here is a small test that shows a list of all the pages in your site
function doGet() {
var app=UiApp.createApplication().setTitle("Directory of this site")
var mainPanel = app.createAbsolutePanel().setStyleAttributes({background:'beige',padding:'20px'});
var scroll = app.createScrollPanel().setPixelSize(450,300);
var site = SitesApp.getSiteByUrl('https://sites.google.com/site/appsscriptexperiments/')
var pages = site.getAllDescendants()
var grid = app.createGrid(pages.length,2).setWidth(400)
for(n=0;n<pages.length;++n){
grid.setText(n, 0, pages[n].getName()).setWidget(n, 1, app.createAnchor('open', pages[n].getUrl()))
}
app.add(mainPanel.add(scroll.add(grid)))
return app
}
I suppose you know ho to use it in your sitepage but just for info for anyone else : you have to go to manage site > script apps > create a new script > save a version > deploy with appropriate authorizations and finally return to your page and insert it as a gadget (better with a border and a size of 450x300, personal pov ^^) . You can also use it as a standalone webapp using its deploy url.

Embedding Google drive in a Google page

I am trying to show a specific privately shared drive folder in my Google page. I am using this guide: https://developers.google.com/apps-script/articles/embedding_docslist_in_sites
I am stuck on Step "8. Replace PASTE_PAGE_URL_HERE in the method getPageByUrl() by the url of your file cabinet."
I have tried every URL combination in "" and I just get a scripting error on the getPageByUrl() method.
I just tested this and it seems to work fine for me. Make sure you are using the full URL for your file cabinet page. For example, on my test site, I created a new page using 'file cabinet' in the template dropdown calling it "myfcpage". This gives me a new page with URL: "https://sites.google.com/site/mytestsite/myfcpage". Therefore my function call looks like this:
var page = SitesApp.getPageByUrl("https://sites.google.com/site/mytestsite/myfcpage");

Find original url of links rather than the tracking url from google, facebook etc

I am making a chrome extension for pdf links
function getD(info, tab)
{
var url = info.srcUrl;
var sb = "http://www.abcfadsds.com/?url="+info.linkUrl;
chrome.tabs.create({"url":sb});
}
Now this works for most links but if a right click a link on google / facebook
the url generated is something like
http://www.google.co.in/url?sa=t&rct=j&q=pdf&source=web&cd......................
how do i obtain the original URL??
Usually its a variable in the url. For instance in a google links youll see something like....
url=http%3A%2F%2Fwww.aaa.com%2F
...so you just need to get the value for url from the link and unescape it.
Theres a great bit of JS for parsing a url here...
http://blog.stevenlevithan.com/archives/parseuri
...using that you could do (for a google link)...
url = parseUri('http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.aaa.com%2F&ei=g2p1T4u6HcSoiAeO8-DYDg&usg=AFQjCNECtBRRPOF1ooUuuMtNR5y7DSChGQ');
link = unescape(url.queryKey.url);