Google Script to open URL in same window as Spreadsheet - hyperlink

I'm trying to work around the lack of API for Google Spreahsheet's Filter Views, by passing the filter view's URL into a hyperlink displayed in a sidebar.
Importantly: I want the filter view URL to open in the same window as, and thus replace, the spreadsheet. The hyperlink target should then be _self
function listFilterViews(){
var uiInstance = UiApp.createApplication()
.setTitle('Teacher Views');
var panel = uiInstance.createVerticalPanel();
panel.setSpacing(5)
var scroll = uiInstance.createScrollPanel();
scroll.setHeight("100%")
var url = "https://docs.google.com/blablabla"
var link = uiInstance.createAnchor("click me", url)
link.setTarget("_self")
panel.add(link);
scroll.add(panel)
uiInstance.add(scroll);
SpreadsheetApp.getUi().showSidebar(uiInstance);
}
However, the URL doesn't open in the same window as expected but in an other window instead. How can I fix this?

According to the setTarget documentation:
"(setting a target to _self) will only work if the app is being shown as a standalone service. It will have no effect when called inside a dialog, such as in a Google Spreadsheet."
Apps Script intentionally prevents scripts from being able to switch the view of the current window, as a security measure.

Related

Getting the current page's window with the low level api of the Firefox SDK

I'm creating a Firefox extension for adding some functionality to certain Web pages. I need to check that some elements do exist and highlight them, so I'm using xpath to check and locate them. I know about manipulating tabs and the content through tabs and ports, but I really need to use the low level API and do it without ports. The thing is, I don't know how to get the current opened tab window (I can also open the tab, but I'm not getting the window). I already tryed to open a tab and :
tabs.open({
url: url,
onOpen: function onOpen(tab) {
// get the XUL tab that corresponds to this high-level tab
var lowLevelTab = viewFor(tab);
var browser = tab_utils.getBrowserForTab(lowLevelTab);
var doc = browser.contentDocument;
console.log(doc); //THIS IS AN EMPTY DOC
// get the most recent window. This give me a XUL window, and I can't sucessfully execute eval on that...
var win = utils.getMostRecentBrowserWindow();
}})
I sawa lot of methods for retrieving different kinds of windows, but I'm not finding the explanation about the differences. E.g. Chroe window, XUL window, NSI window, base window...I just need the current Web page's document window.
Any clarification is welcome.
Thanks in advance,
I just needed to listen for another tab event:
onReady: function onOpen(tab) {
var content = utils.getMostRecentBrowserWindow().content;
var domInstances = content.document.evaluate(me.getTemplateXpath(), content.document, null, 4, null);
var res = domInstances.iterateNext();
while (res) {
console.log(res);
res.style["background-color"] = "orange";
res = domInstances.iterateNext();
}
callback();
}

Open URL with a set Target

EDIT:
The solution I am seeking is a command line to be run to accomplish this from a batch file in Windows.
How would I mimic a browser function to open a URL with a specific target so that if that tab is already open it will load there instead of creating a new tab?
So instead of the default http://url.com with the target "_blank", I could change this to "w00t", etc.
I am using Chrome exclusively, so if this were to be a Chrome specific command, that would be acceptable.
You could create a HTML-page and add the following code like this:
<script>
var url = getQueryVariable("url");
var target = getQueryVariable("target");
window.open(url,target);
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
</script>
I have this script hosted here: scriptcoded.github.io/redirect
You could then call it from a batch-file using this command:
START scriptcoded.github.io/redirect?url=http://www.google.com&target=w00t
Use ?url=http://www.google.com&target=_blank to set the redirect.
The only problems I find with this method is that some browsers blocks the new window and that the tab won't close. Yes, we could use window.close();, but it will only close tabs that were opened by the webpage itself.
Hope it helps!
set name for html tag of target page like this:
<html name="w00t">
Note: A link to URL with target name, sets the name for target window automatically:
Google

Bookmarklet to save URL in Google Spreadsheet

I want to create a simple bookmarklet, that grabs the URL of the current webpage "location.ref" and saves it in a Google Spreadsheet. After it saves it, I want to stay on the current webpage.
The only way I know of writing to Google Spreadsheet is using Google App Script. So I wrote a simple script that does just that:
function doGet(request) {
var ss = SpreadsheetApp.openByUrl( "https://docs.google.com/spreadsheet/ccc?key=<MY-SPREADSHEET-ID>");
var sheet = ss.getSheets()[0];
var headers = ["Timestamp", "url"];
var nextRow = sheet.getLastRow();
var cell = sheet.getRange('a1');
var col = 0;
for (i in headers){
if (headers[i] == "Timestamp"){
val = new Date();
} else {
val = request.parameter[headers[i]];
}
cell.offset(nextRow, col).setValue(val);
col++;
}
return ContentService.createTextOutput(request.parameter.url)
.setMimeType(ContentService.MimeType.TEXT);
}
I published this as a webapp. I wrote the bookmarklet:
<a href="javascript:(
function(){
alert(window.open('https://script.google.com/macros/s/<MYWEBAPP>/exec?url='+encodeURIComponent(location.href), '_self'));
}
)();">
BOOKMARK
</a>
So far so good. It actually works when I click on the bookmarklet, it does grab the URL of the current webpage and save it in my spreadsheet. But then, the webapp returns a text response and the bookmarklet displays the text causing me to move away from my current website.
Is there a way to ignore the response? GAS webapp script requires me to use doGet() that has to return something. Is there a way to not return anything from GAS script? Alternatively, is there a way i could use some other call to replace window.open to invoke the webapp that would allow me to store the response in a variable and ignore it?
I know it's been over a year but I was trying to do exactly this. It took me a while to figure out, but this works. The 1 second delay was necessary to let the script finish loading.
javascript:(function(){
my_window=window.open('https://script.google.com/macros/s/<MYWEBAPP>/exec?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));
(window.setTimeout(function(){my_window.close();},1000));
void(0);
})();
Instead of using window.open you may consider sending a HTTP GET request using XMLHttpRequest.
Refer here on its usage.
Change _self to something else, e.g. bookmarker and it will open in a new window or tab. If you use it on many pages, they will all reuse the same tab if it keeps the same name.

Text URL in AIR iOS app not selectable

I'm using AIR 2.0 (soon will be updating to 3.3 with Flash CS6) to create an iPad app. We have textfields (Classic, dynamic) which sometimes contain one or multiple htmlText links which need to be clickable. In the desktop version of the program, all text is selectable and the links are easily accessed. My problem is that it takes me mashing the link like 20 times on the iPad before it will recognize that there's a link and navigate to it in Safari. The other strange thing is that none of the text appears to be selectable - I can't get the iPad cursor, copy/paste menu, etc. to show up.
I think, from reading other threads, that the hit area for the URL is only the stroke on the text itself... if that's true, what can I do to increase the hit area? Or make text selectable? It was suggested elsewhere to put movieclips behind the URLs but that's not really possible as this is all dynamic text from XML files.
I've read about StageText but I gather this is only used for input fields, which is not the case here.
I'm reasonably advanced in AS3 but I'd prefer an easy solution over re-writing large chunks of code. At the moment the only thing I can think to do is get the URL and make it so that as soon as you touch anywhere on the textfield, it navigates to the link. But this would break down if there were more than 1 URL in a given textfield.
Any ideas?
I had this exact same issue, and it's had me flummoxed for a while.
Here's what I did to get the desired behaviour:
1) Instead of using a listener for TextEvent.LINK, listen for MouseEvent.CLICK (or TouchEvent.TAP) on the TextField.
eg.
var tf:TextField = new TextField();
tf.addEventListener(MouseEvent.CLICK, linkClicked);
2) In the linkClicked() handler, you use getCharIndexAtPoint() to determine the index of the character that was clicked, and then from that determine the URL from the TextFormat of the character. This is adapted from a post by Colin Holgate on the Adobe Forums (http://forums.adobe.com/thread/231754)
public function linkClicked(e:MouseEvent):void {
var idx:int = e.target.getCharIndexAtPoint(e.localX, e.localY);
trace("Tapped:",idx);
var tf:TextFormat = e.target.getTextFormat(idx);
if(tf.url != "" && tf.url != null) {
var linkURL:String = tf.url;
trace(linkURL);
// Hyperlink processing code here
dispatchEvent(new UIEvent(UIEvent.LINK_TAPPED,tf.url));
}
}
3) The last line (dispatchEvent()) is sending a custom event to another function to process the link, but you could easily inline your code here.
I've tested on an iPad 3 running iOS6.1, building with AIR3.5. Links are much more responsive, and I don't find myself mashing the screen trying to hit the stroke of the text!

Disable JavaScript in a single Firefox tab

In a Firefox add-on built with the Add-on SDK, how can I disable and re-enable JavaScript for a single tab?
The SDK itself doesn't provide this functionality, you will have to work with the XUL directly. What you need to do is accessing the docShell property of the XUL <browser> element corresponding to the tab. The docshell has an allowJavascript property that lets you switch JavaScript on and off. Something like this should work:
var window = require("window-utils").activeBrowserWindow;
var tabBrowser = window.gBrowser;
var browser = tabBrowser.selectedBrowser; // or: tabBrowser.browsers[n]
browser.docShell.allowJavascript = false;
Unfortunately, it doesn't seem possible to take a Tab object and find the corresponding XUL element - you have to work with the XUL window from the start.
Relevant documentation:
window-utils package (the properties activeWindow/activeBrowserWindow are undocumented for some reason).
<tabbrowser> element
<browser> element
nsIDocShell interface

Resources