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

[FireMonkey] New API: GM.import() #558

Closed
erosman opened this issue May 22, 2023 · 0 comments
Closed

[FireMonkey] New API: GM.import() #558

erosman opened this issue May 22, 2023 · 0 comments

Comments

@erosman
Copy link
Owner

erosman commented May 22, 2023

New APIs in FireMonkey 2.68 ...

GM.import()

Asynchronous GM.import for importing internal and remote modules

It returns a promise which fulfills to a module namespace object: an object containing all exports from moduleName.

The current format is due to the limitations in userScripts context, as it is not possible to directly pass the class to the userScripts context. The API may change once MV3 scripting is finalised.

// variables must match module exports
const {PSL} = await GM.import('https://raw.githubusercontent.com/erosman/psl/main/psl.js');

const result = PSL.parse('mail.yahoo.co.uk');
// Object { subdomain: "mail", domain: "yahoo.co.uk", sld: "yahoo", tld: "co.uk" }

Internal module: PSL (Public Suffix List)

PSL (Public Suffix List) was created for the multi-site "Find scripts for this site", and has been made available to userscripts.

// import as module
const {PSL} = await GM.import('PSL');

const result = PSL.parse('mail.yahoo.co.uk');
// Object { subdomain: "mail", domain: "yahoo.co.uk", sld: "yahoo", tld: "co.uk" }

import Option

Experimental Import Attributes is supported but the proposal is not finalised yet.

Return value: based on option type

  • json: object
  • css: string
  • html: DocumentFragment
const obj = await GM.import('http://country.io/currency.json', {type: 'json'});
console.log(obj);
// {"BD": "BDT", "BE": "EUR", ... }
const css = await GM.import('https://raw.githubusercontent.com/erosman/support/FireMonkey/content/default.css', {type: 'css'});
console.log(css);
// '/* ----- Dark Theme ----- */\r\n:root,\r\nbody.dark {\r\n  --color: #fff; ...
const docFrag = await GM.import('https://raw.githubusercontent.com/erosman/support/FireMonkey/content/help.html', {type: 'html'});
const div = document.createElement('div');
div.appendChild(docFrag);
console.log(div.firstElementChild);
// <meta charset="utf-8">

See also:

@erosman erosman changed the title [FireMonkey] New Storage API: GM.import() [FireMonkey] New API: GM.import() May 23, 2023
@erosman erosman closed this as completed Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant