Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added my "Geolocation" extensions #725

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions extensions/SamuelLouf/geolocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*!
* Copyright 2023 SamuelLouf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

(function (Scratch) {
'use strict';

Check warning on line 19 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
const delay = ms => new Promise(res => setTimeout(res, ms));

Check warning on line 21 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};

Check warning on line 27 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
function success(pos) {
const crd = pos.coords;
TurboWarpGeolocationExtension_coordinates = {

Check failure on line 30 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

'TurboWarpGeolocationExtension_coordinates' is not defined
success: true,
latitude: crd.latitude,
longitude: crd.longitude,
accuracy: crd.accuracy
};
localStorage.TurboWarpGeolocationExtension_coordinates = JSON.stringify(TurboWarpGeolocationExtension_coordinates);

Check failure on line 36 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

'TurboWarpGeolocationExtension_coordinates' is not defined
}

Check warning on line 38 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
function error(err) {
TurboWarpGeolocationExtension_coordinates = {

Check failure on line 40 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

'TurboWarpGeolocationExtension_coordinates' is not defined
success: false,
error: {
code: err.code,
message: err.message
}
};
localStorage.TurboWarpGeolocationExtension_coordinates = JSON.stringify(TurboWarpGeolocationExtension_coordinates);

Check failure on line 47 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

'TurboWarpGeolocationExtension_coordinates' is not defined
}

async function getGeolocation() {
localStorage.TurboWarpGeolocationExtension_coordinates = '{}'

Check warning on line 51 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
navigator.geolocation.getCurrentPosition(success, error, options);
while (localStorage.TurboWarpGeolocationExtension_coordinates == '{}'){
await delay(100);
}
return JSON.parse(localStorage.TurboWarpGeolocationExtension_coordinates);
}

const icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxMTAuODAyNSIgaGVpZ2h0PSIxMTAuNDAyNDkiIHZpZXdCb3g9IjAsMCwxMTAuODAyNSwxMTAuNDAyNDkiPjxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0xODQuNTk4NzUsLTEyNC43OTg3NSkiPjxnIGRhdGEtcGFwZXItZGF0YT0ieyZxdW90O2lzUGFpbnRpbmdMYXllciZxdW90Ozp0cnVlfSIgc3Ryb2tlLWxpbmVjYXA9ImJ1dHQiIHN0cm9rZS1saW5lam9pbj0ibWl0ZXIiI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zLjcyLC02LjE0MjY1IC0xMy43MiwtMTMuNzJjMCwtNy41NzczNSA2LjE0MjY1LC0xMy43MiAxMy43MiwtMTMuNzJ6IiBmaWxsPSIjMDM2ZTE1IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZT0iIzAwYTExYiIgc3Ryb2tlLXdpZHRoPSIxLjUiLz48L2c+PC9nPjwvc3ZnPjwhLS1yb3RhdGlvbkNlbnRlcjo1NS40MDEyNTAwMDAwMDAwMDU6NTUuMjAxMjQ1LS0+';

class Geolocation {
getInfo() {
return {
id: 'samuelloufgeolocation',
name: 'Geolocation',
color1: '#036e15',
color2: '#00A11B',
menuIconURI: icon,
blocks: [
{
opcode: 'is_geolocation_supported',
blockType: Scratch.BlockType.BOOLEAN,
text: 'is geolocation supported by this device?',
},
{
opcode: 'is_geolocation_allowed',
blockType: Scratch.BlockType.BOOLEAN,
text: 'is geolocation allowed?',
},
{
opcode: 'get_current',
blockType: Scratch.BlockType.REPORTER,
text: 'get [WHAT]',
arguments: {

Check warning on line 84 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
WHAT: {
type: Scratch.ArgumentType.REPORTER,
menu: 'get_current_menu',
defaultValue: 'longitude'
}
}
},
{
opcode: 'set_timeout_to_value',
blockType: Scratch.BlockType.COMMAND,
text: 'set timeout to [SECONDS] seconds',
arguments: {

Check warning on line 96 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
SECONDS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 5
}
}
},
{
opcode: 'add_value_to_timeout',
blockType: Scratch.BlockType.COMMAND,
text: 'add [SECONDS] seconds to timeout',
arguments: {

Check warning on line 107 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
SECONDS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
}
}
},
{
opcode: 'get_timeout',
blockType: Scratch.BlockType.REPORTER,
text: 'get timeout'
}
],
menus: {
get_current_menu: {
acceptReporters: true,
items: ['longitude', 'latitude', 'accuracy']
}
}
};
}

Check warning on line 128 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
async get_current (args) {
var coordinates = await getGeolocation();
if (coordinates.success == true){
return coordinates[args.WHAT];
} else {
return "Error: " + coordinates.error.message;
}
}

async is_geolocation_allowed () {
var coordinates = await getGeolocation();
if (coordinates.success == false && coordinates.error.code == 1) {
return false;
} else {
return true;
}
}

async is_geolocation_supported () {

Check failure on line 147 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Async method 'is_geolocation_supported' has no 'await' expression
if (navigator.geolocation){
return true;
} else {
return false;
}
}

set_timeout_to_value (args) {
options.timeout = Number(args.SECONDS)*1000;

Check warning on line 156 in extensions/SamuelLouf/geolocation.js

View workflow job for this annotation

GitHub Actions / build

Operator '*' must be spaced
}

add_value_to_timeout (args) {
options.timeout = options.timeout + Number(args.SECONDS)*1000;
}

get_timeout () {
return options.timeout/1000;
}
}
Scratch.extensions.register(new Geolocation());
})(Scratch);
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@
<h3>McUtils</h3>
<p>Helpful utilities for any fast food employee. Created by <a href="https://scratch.mit.edu/users/LilyMakesThings/">LilyMakesThings</a>.</p>
</div>

<div class="extension">
<%- banner('SamuelLouf/geolocation.js') %>
<h3>Geolocation</h3>
<p>Various geolocation tools. Created by <a href="https://scratch.mit.edu/users/samuellouf/">SamuelLouf</a>.</p>
</div>
</div>
</div>

Expand Down
Loading