diff --git a/extensions/SamuelLouf/geolocation.js b/extensions/SamuelLouf/geolocation.js new file mode 100644 index 0000000000..759dffb712 --- /dev/null +++ b/extensions/SamuelLouf/geolocation.js @@ -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'; + + const delay = ms => new Promise(res => setTimeout(res, ms)); + + const options = { + enableHighAccuracy: true, + timeout: 5000, + maximumAge: 0, + }; + + function success(pos) { + const crd = pos.coords; + TurboWarpGeolocationExtension_coordinates = { + success: true, + latitude: crd.latitude, + longitude: crd.longitude, + accuracy: crd.accuracy + }; + localStorage.TurboWarpGeolocationExtension_coordinates = JSON.stringify(TurboWarpGeolocationExtension_coordinates); + } + + function error(err) { + TurboWarpGeolocationExtension_coordinates = { + success: false, + error: { + code: err.code, + message: err.message + } + }; + localStorage.TurboWarpGeolocationExtension_coordinates = JSON.stringify(TurboWarpGeolocationExtension_coordinates); + } + + async function getGeolocation() { + localStorage.TurboWarpGeolocationExtension_coordinates = '{}' + 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,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxMTAuODAyNSIgaGVpZ2h0PSIxMTAuNDAyNDkiIHZpZXd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jE5LDE2IDMuMzEsMjUuMTJjLTMuMTgyMzQsMTQuNjIxNzcgLTEyLjE1NTQ4LDI3LjMyOTIyIC0yNC44NywzNS4yMnpNMjQwLjAwNzE0LDE1Ny40Nzc3MWM3LjU3NzM1LDAgMTMuNzIsNi4xNDI2NSAxMy43MiwxMy43MmMwLDcuNTc3MzUgLTYuMTQyNjUsMTMuNzIgLTEzLjcyLDEzLjcyYy03LjU3NzM1LDAgLTEzLjcyLC02LjE0MjY1IC0xMy43MiwtMTMuNzJjMCwtNy41NzczNSA2LjE0MjY1LC0xMy43MiAxMy43MiwtMTMuNzJ6IiBmaWxsPSIjMDM2ZTE1IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZT0iIzAwYTExYiIgc3Ryb2tlLXdpZHRoPSIxLjUiLz48L2c+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: { + 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: { + SECONDS: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: 5 + } + } + }, + { + opcode: 'add_value_to_timeout', + blockType: Scratch.BlockType.COMMAND, + text: 'add [SECONDS] seconds to timeout', + arguments: { + 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'] + } + } + }; + } + + 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 () { + if (navigator.geolocation){ + return true; + } else { + return false; + } + } + + set_timeout_to_value (args) { + options.timeout = Number(args.SECONDS)*1000; + } + + 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); \ No newline at end of file diff --git a/website/index.ejs b/website/index.ejs index fd3812c909..bf6cedc38f 100644 --- a/website/index.ejs +++ b/website/index.ejs @@ -745,6 +745,12 @@
Helpful utilities for any fast food employee. Created by LilyMakesThings.
+ +Various geolocation tools. Created by SamuelLouf.
+