mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-28 13:39:44 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
/**
|
|
* Prints a list of tasks on a list with a specified |listId|.
|
|
*/
|
|
function printTasks(listId, jsonResp, rawResp) {
|
|
if (jsonResp) {
|
|
var taskListsList = document.querySelector('#' + listId);
|
|
jsonResp.items.forEach(function(item) {
|
|
var entry = document.createElement('li');
|
|
entry.textContent = item.title;
|
|
taskListsList.appendChild(entry);
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets all of the tasks on a task list identitfied by a specified |listId| and
|
|
* then prints them.
|
|
*/
|
|
function getTasksOnList(listId) {
|
|
gapi.client.request({
|
|
'path': '/tasks/v1/lists/' + listId + '/tasks',
|
|
'callback': printTasks.bind(null, listId)
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Takes the |jsonResp| and prints all of the lists of tasks found in the items
|
|
* property.
|
|
*/
|
|
function printTaskLists(jsonResp, rawResp) {
|
|
if (jsonResp && jsonResp.items && jsonResp.items.length > 0) {
|
|
var documentBody = document.querySelector("body");
|
|
jsonResp.items.forEach(function(item) {
|
|
var listHeader = document.createElement("h2");
|
|
listHeader.textContent = item.title;
|
|
documentBody.appendChild(listHeader);
|
|
var list = document.createElement("ul");
|
|
list.id = item.id;
|
|
documentBody.appendChild(list);
|
|
getTasksOnList(item.id);
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the list of task lists owned by the user.
|
|
*/
|
|
function getListsOfTasks() {
|
|
gapi.client.request({
|
|
'path': '/tasks/v1/users/@me/lists',
|
|
'callback': printTaskLists
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Prompts the user for authorization and then proceeds to
|
|
*/
|
|
function authorize(params, callback) {
|
|
gapi.auth.authorize(params, function(accessToken) {
|
|
if (!accessToken) {
|
|
var error = document.createElement("p");
|
|
error.textContent = 'Unauthorized';
|
|
document.querySelector("body").appendChild(error);
|
|
} else {
|
|
callback();
|
|
}
|
|
});
|
|
}
|
|
|
|
function gapiIsLoaded() {
|
|
var params = { 'immediate': false };
|
|
if (!(chrome && chrome.app && chrome.app.runtime)) {
|
|
// This part of the sample assumes that the code is run as a web page, and
|
|
// not an actual Chrome application, which means it takes advantage of the
|
|
// GAPI lib loaded from https://apis.google.com/. The client used below
|
|
// should be working on http://localhost:8000 to avoid origin_mismatch error
|
|
// when making the authorize calls.
|
|
params.scope = "https://www.googleapis.com/auth/tasks.readonly";
|
|
params.client_id = "966771758693-dlbl9dr57ufeovdll13bb0evko6al7o3.apps.googleusercontent.com";
|
|
gapi.auth.init(authorize.bind(null, params, getListsOfTasks));
|
|
} else {
|
|
authorize(params, getListsOfTasks);
|
|
}
|
|
}
|