Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***** manifest.json *****/
- {
- "manifest_version": 3,
- "name": "Add page to Things",
- "version": "1.0",
- "description": "Captures the current page title and URL and creates a new entry in the Things inbox",
- "permissions": [
- "tabs",
- "clipboardRead"
- ],
- "background": {
- "scripts": [
- "background.js"
- ]
- },
- "action": {
- "default_title": "Add to Things"
- },
- "commands": {
- "capture-page": {
- "suggested_key": {
- "default": "Command+Shift+Space"
- },
- "description": "Add to Things"
- }
- }
- }
- /***** background.js *****/
- browser.action.onClicked.addListener(async (tab) => {
- if (!tab || !tab.url || !tab.title) {
- return;
- }
- let clipboardText = "";
- try {
- const clipboardText = await navigator.clipboard.readText();
- console.log('Pasted content: ', clipboardText);
- } catch (err) {
- console.error("Failed to read clipboard:", err);
- alert( err );
- clipboardText = "";
- }
- const pageTitle = tab.title;
- const pageUrl = tab.url;
- // Things URL API:
- // https://culturedcode.com/things/support/articles/2803573/
- // open "things:///add?title=$TITLE¬es=$URLTD"
- // If processing multiple items, might need to massage like this:
- // $urlencoded = urlencode( json_encode($json_src) );
- // $urlencoded = str_replace("%22%3A%22", "%22:%22", $urlencoded); # swap : in where %3A appears between quotes
- // $urlencoded = str_replace("+", "%20", $urlencoded); # replace '+' with '%20'
- // $command = "open things:///json?data=" . $urlencoded;
- // Encode values safely for URLs
- const encodedTitle = encodeURIComponent(pageTitle);
- const encodedUrl = encodeURIComponent(pageUrl);
- // Construct your target URL
- // Example: sending title + URL as query parameters
- const targetUrl = `things:///add?title=${encodedTitle}¬es=${encodedUrl}`;
- async function openAndAutoCloseTab() {
- // 1. Open the tab
- const tab = await browser.tabs.create({ url: targetUrl, active: false });
- const tabId = tab.id;
- // 2. Listener for when the tab finishes loading
- function onUpdated(updatedTabId, changeInfo) {
- if (updatedTabId === tabId && changeInfo.status === "complete") {
- // Remove listener so it only fires once
- browser.tabs.onUpdated.removeListener(onUpdated);
- // 3. Wait 1 second, then close the tab
- setTimeout(() => {
- browser.tabs.remove(tabId).catch(() => {
- // Tab may already be closed; ignore errors
- });
- }, 1000);
- }
- }
- browser.tabs.onUpdated.addListener(onUpdated);
- }
- openAndAutoCloseTab();
- // // Open the new URL in a new tab
- // const ThingsTab = await browser.tabs.create({
- // url: targetUrl,
- // active: false
- // });
- // // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/create
- // // ThingsTab.close();
- });
Advertisement
Add Comment
Please, Sign In to add comment