Skip to content

Instantly share code, notes, and snippets.

@nikhan
Last active July 14, 2020 04:53
Show Gist options
  • Save nikhan/db148ad236f485fce806 to your computer and use it in GitHub Desktop.
Save nikhan/db148ad236f485fce806 to your computer and use it in GitHub Desktop.
Share Whatever over Google Tone
// send whatever message you want over Google Tone
// as far as I can tell, profile images must live on googleusercontent.com
// data.url must look like a url
// setting AUDIBLE_DURATION to 1 will generate no audible sound but result in a successful transmission.
//
// to use:
// 0. install Google Tone https://chrome.google.com/webstore/detail/google-tone/nnckehldicaciogcbchegobnafnjkcne
// 1. open settings > extentions
// 2. click "options" under Google Tone
// 3. In the Google Tone options, open the developer console and paste the following
var AUDIBLE_ID = 'shoutout_audible';
var INAUDIBLE_ID = 'shoutout_inaudible';
var SUBSCRIBE_ID = 'subscribe_shoutout';
var MESSAGE_TYPE = 'shoutout';
// Events which CopresenceManager exposes.
var VALID_EVENTS = ['sent', 'received', 'unsubscribed', 'error'];
var getPublishId_ = function(audible) {
return audible ? AUDIBLE_ID : INAUDIBLE_ID;
};
var PUBLISH_TEMPLATE = {
publish: {
id: AUDIBLE_ID,
message: {
type: MESSAGE_TYPE,
payload: null // Replace with data.
},
timeToLiveMillis: AUDIBLE_DURATION,
strategies: {
onlyBroadcast: true,
audible: true // Replace with whether or not it's audible.
}
}
};
var getPublishTemplate_ = function(params) {
// Params are {audible: boolean, payload: JSON string, timeToLiveMillis: int}.
params = params || {};
var payload = params.payload;
var audible = !!params.audible;
var timeToLiveMillis = params.timeToLiveMillis;
if (payload == null) {
return console.error('No payload specified!');
}
var textEncoder = new TextEncoder();
var obj = JSON.parse(JSON.stringify(PUBLISH_TEMPLATE))
obj.publish.message.payload = textEncoder.encode(JSON.stringify(payload)).buffer;
obj.publish.strategies.audible = audible;
if (timeToLiveMillis) {
obj.publish.timeToLiveMillis = timeToLiveMillis;
}
obj.publish.id = getPublishId_(audible);
return obj;
};
var AUDIBLE_DURATION = 1750; // 14 * 125.
var INAUDIBLE_DURATION = 5000;
var data = {
url: "http://HELLO/",
name: "HELLO NAME",
picture: "http://lh5.googleusercontent.com/-mrbGiXcK0HA/VVt9kB9yHtI/AAAAAAAAAl4/f95e0uRPkTE/w1280-h800-no/lol.gif",
};
tab = {title: "WHATEVER"}
if (tab.title) {
data.title = tab.title;
}
var audible = this.getPublishTemplate_({
payload: data,
audible: true,
timeToLiveMillis: AUDIBLE_DURATION
});
var inaudible = this.getPublishTemplate_({
payload: data,
audible: false,
timeToLiveMillis: INAUDIBLE_DURATION
});
chrome.copresence.execute([inaudible, audible], function(){});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment