How to replace chrome.tabs.onSelectionChanged deprecated method? - google-chrome

I need to get data from the tab the user is leaving (switching on other tab or going on other program).
But chrome.tabs seems not providing an event allowing that..
Before there was apparently chrome.tabs.onSelectionChanged (not tested) but it's deprecated.
And other event are giving the data of the new tab not the one the user just left...
I try also jQuery $(window).blur event, but i have to make a call to the chrome.storage of the tab left by the user (i create a storage for each tab named by the tab id) and i did not get the response of storage in time with this event (the result of the storage is used in a if() to know if i have or not to display an confirm box.
Someone could help me ?
Thx !

To detect a tab change, just use chrome.tabs.onActivated. Since you're interested in the previous tab, store the result of the event at the end of each event. For instance, like this:
var storedWindowInfo = {};
chrome.tabs.onActivated.addListener(function(activeInfo) {
var windowLastTabId = storedWindowInfo[activeInfo.windowId];
if (windowLastTabId) {
// Do something with previous tab, e.g. send a message:
chrome.tabs.sendMessage(windowLastTabId);
}
// Update ID of currently active tab in the current window
storedWindowInfo[activeInfo.windowId] = activeInfo.tabId;
});
Note: Only the tabID and windowID are provided to this event. You need to issue chrome.tabs.query to get more information, such as whether the tab still exists, its URL, etc.

Related

How to make a google form using apps script

I want to make a google form in apps script that will be like an inventory check out/in form. I want all the options to be under the check out section and every time a response gets submitted for checking out an item, the next time the form opens that item will appear under the check in section. I also want the item to go from check in to check out as well but I'm not even sure that functionality is available, just thought I'd put it out there. I started creating the form but I'm not sure where to go from here, the documentation for apps script isn't the most detailed.
//variable for new form
var newForm = FormApp.create('CS IT Checkout Form test');
//making name field
var name = newForm.addTextItem();
//variable for email
var email = newForm.addTextItem();
//variable for checkout checkboxes
var checkBoxItem = newForm.adCheckBoxItem();
//variable for checkin checkboxes
var checkBoxItem2 = newForm.addCheckboxItem();
function myFunction() {
//making description
newForm.setDescription('Checkout and check-in form for CS tech');
//making name field
//at some point make the name field required or just do it in actual
form
name.setTitle('Name');
//making email field
email.setTitle('Email')
//making checkout equipment fields
checkoutItem.setTitle('Check Out');
//default options until responses are made then it begins to change
checkBoxItem.setChoiceValues(['Laptop', 'Tablet', 'Monitor', 'Camera']);
//once form submissions start values will be put in the check in section
checkBoxItem2.setTitle('Check In');
checkBoxItem2.setChoiceValues();
}
Writing this as an answer rather than a comment because formatting.
First you have to figure out a flow that would work for what you want. Does this sound about right?
I think you need a form, and an inventory spreadsheet connected to that form.
The form asks the user whether it is a check/check out choice, and then the user select the appropriate item from the correct dropdown (check in or check out). I think you can tie this logic to a first page yes/no, but I haven't tried it. So worst case scenario three questions and they leave one dropdown blank. tie the form to the spreadsheet.
You need an onsubmit function in the spreadsheet that does the following:
Determine whether the form was checking in or checking out
Move the item to the appropriate sheet (two otherwise identical sheets for
items that are checked in, and checked out).
Update the values for the form with the right items in the dropdown
Now it is ready for the next usage. Is this what you envision?
Actually I didn't need to store the information in a sheet, I figured out how to do it by making a function that gets the latest response and passes it to another function that creates a new choice for the new section

chromeapp clear all notifications

I am trying to create a chromeapp that, when a hotkey is pressed, clears all notifications. I have the hotkey set up and working, but I can't seem to get the chrome.notifications.clear api to work, and I think it is because I can't/don't know how to get all notification ids. Is there any way to clear a notification without knowing its id, or just clear all notifications? Thanks!
Based on the documentation,
you need to get the notificationId to delete the notification.
The chrome.notifications.clear(string notificationId, function callback) it only clears a specified notification.
The id of the notification to be cleared is returned by notification.create method.
So if you dont know the notificationId in the system, you can get it by calling the chrome.notifications.getAll(function callback). It retrieves all the notification and notificationId in the system.
If someone still needs a code example:
chrome.notifications.getAll((items) => {
if ( items ) {
for (let key in items) {
chrome.notifications.clear(key);
}
}
});

IndexedDB: When to close a connection

I would like to know what the correct place to close a connection to the database is.
Let's say that I have the following piece of code:
function addItem(dbName, versionNumber, storeName, element, callback){
var requestOpenDB = indexedDB.open(dbName, versionNumber); //IDBRequest
requestOpenDB.onsuccess = function(event){
//console.log ("requestOpenDB.onsuccess ");
var db = event.target.result;
var trans = db.transaction(storeName, "readwrite");
var store = trans.objectStore(storeName);
var requestAdd = store.add(element);
requestAdd.onsuccess = function(event) {
callback("Success");
};
requestAdd.onerror = function(event) {
callback("Error");
};
};
requestOpenDB.onerror = function(event) {
console.log ("Error:" + event.srcElement.error.message);/* handle error */
callback("Error");
};
}
addItem basically adds a new element into the database. As per my understanding, when the requestAdd event is triggered that doesn't mean necessarily that the transaction has finished. Therefore I am wondering what the best place to call db.close() is. I was closing the connection inside of requestAdd.onsucess, but if an error happens and requestAdd.onerror is triggered instead, the connection might still be opened. I am thinking about adding trans.oncomplete just under request.onerror and close the db connection here which might be a better option. Any inputs will be more than welcome. Thank you.
You may wish to explicitly close a connection if you anticipate upgrading your database schema. Here's the scenario:
A user opens your site in one tab (tab #1), and leaves it open.
You push an update to your site, which includes code to upgrade the database schema, increasing the version number.
The same user opens a second tab to your site (tab #2) and it attempts to connect to the database.
If the connection is held open by tab #1, the connection/upgrade attempt by tab #2 will be blocked. Tab #1 will see a "versionchange" event (so it could close on demand); if it doesn't close its connection then tab #2 will see a "blocked" event.
If the connection is not held open by tab #1, then tab #2 will be able to connect and upgrade. If tab #1 then tries (based on user action, etc) to open the database (with an explicit version number) it will fail since it will be using an old version number (since it still has the old code).
You generally never need to close a connection. You are not creating memory leaks or anything like that. Leaving the connection open does not result in a material performance hit.
I would suggest not worrying about it.
Also, whether you add trans.oncomplete before or after request.onerror is not important. I understand how it can be confusing, but the order in which you bind the listeners is irrelevant (qualified: from within the same function scope).
You can call db.close() immediately after creating the transaction
var trans = db.transaction(storeName, "readwrite");
db.close();
and it will close the connection only after the transaction has completed.
https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close says
The connection is not actually closed until all transactions created using this connection are complete. No new transactions can be created for this connection once this method is called.
If you want to run multiple versions of your app and both access the same database, you might think it's possible to keep connections open to both. This is not possible. You must close the database on one before opening it on another. But one problem is that there is currently no way to know when the database actually closes.

getTag() method not properly working for google apps script

What i am doing in writing a script that lets the User interact with a data table. Every series that the user chooses creates a button, and then plots the series on a graph. if the user click the button it rooms the series. All there the data is stored in a hidden JSON string. the columns, or series that the user whats to see are stored in an array that i call index, it is also a hidden JSON string) Each button is connected to its own client handler, which has a
.forTargets(the index i was talking about).setTag(to the corresponding column in the data array)
and they are all connected to the same server handler. So when the button is clicked the client handler sets the tag for the index to the series that it is supposed to be removed. Now the server handler will run it get the index by ID and get the Tag. This is were it goes wrong. The tag is always null.
The first thing i tried was to see if my client handler was not working properly. So i set the tag of the index to some number, but the getTag method in the Server handler still returned null.
idk, but maybe Tags are not saved in the UI instance??
Below is the first bit of the Server handler.
function clickServer(e) {
e = e.parameter;
var app = UiApp.getActiveApplication();
var master = JSON.parse(e.dataTable, "reviver");
var index = JSON.parse(e.index, "reviver");
var hidden = app.getElementById("hiddenIndex");
var tag = hidden.getTag();
I think the issue you are meeting is more related to timing : handlers are called simultaneously, this is true for client an server handlers as well, that means that if the client handler changes a hidden tag value this change happens too late for the server handler function to 'see' it. What you should do is create a separate button to trigger the server handler that the user would use after all the other parameters where set.
This very same method is used in the known workaround used to get radioButtons value
Also, why do you use tags on the hidden widget ? you could use it with direct access by setValue() and e.parameter.hiddenName since they are already invisible by nature... ?
Note also that you can set a value in client handlers as long a these values are defined inside the Ui instance (the do Get function) either by constant variables or by another client Handler in the same function, as shown in the before mentioned example with radioButtons... but that's only a detail.
In this context if you need to get a widget value (inside the doGet function) you should of course use getValue() to get the widget value as no e.parameter is available at this time.
I hope I've been clear enough, re-reading this I'm not very sure but.... just ask if something is missing ;-)
The tags values are passed to handlers via parameters. In this post this fact is explained in details.

Titanium: How to reload a tab upon focus?

I have built a favorite-tab in my application that displays entries in a database that are flagged as such. On the backside this works as it should, but I can't figure out how to reload the favorites tab after changes have been made. When I quit and relaunch the app though, the updates appear as they should.
In essence I have the same problem as is presented here: http://developer.appcelerator.com/question/31211/tab---tableview--database-reload
"I have a 'search' and 'search history' tab in my application. Every search executed under this tab gets inserted into my local db.
However the new search is not reflected in the tableview retrieving data from the DB in the 'search history' tab when i browse to it, after searching.
The latest search, however, gets loaded when I exit the app and relaunch it.
I need some help in figuring out how to trigger the tableview reload without exiting the app.
SOLUTION UPDATE:
I feel 'noobish' for asking this question but it was really so simple
I added a 'focus' eventlistener for the window housing the tab to load the data.
win.addEventListener('focus', function() { loadDBdata(); });
This seems reasonable, but what should the loadDBdata function should contain?
I would greatly appreciate any pointers to this.
i would suggest firing an event when the favorites are updated and having an event listenter on the table that holds the favorites list. When it receives the event, it should update the tables contents.
see this question I just answered it should provide a guide for solving your problem also
Problems with refresh function for page, in Appcelerator
the loaddb function should contain something like:
// init database
var db = Ti.Database.install('path/to/database.db','myDB');
// your request
var sql = 'SELECT * FROM myDatabaseTable';
// the result from that request
var myContent = db.execute(sql);
// need to put the result in a valid structure before inserting into your tableview
var data = [];
while (myContent.isValidRow()){
// your tableviewrow
var row = {
title = myContent.field(0);
};
data.push(row);
myContent.next();
};
// need to close stuff (!)
myContent.close();
db.close();
// finally return data
return data;