Guest User

Untitled

a guest
Jan 23rd, 2026
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /***** manifest.json *****/
  2.  
  3. {
  4. "manifest_version": 3,
  5. "name": "Add page to Things",
  6. "version": "1.0",
  7. "description": "Captures the current page title and URL and creates a new entry in the Things inbox",
  8. "permissions": [
  9. "tabs",
  10. "clipboardRead"
  11. ],
  12. "background": {
  13. "scripts": [
  14. "background.js"
  15. ]
  16. },
  17. "action": {
  18. "default_title": "Add to Things"
  19. },
  20. "commands": {
  21. "capture-page": {
  22. "suggested_key": {
  23. "default": "Command+Shift+Space"
  24. },
  25. "description": "Add to Things"
  26. }
  27. }
  28. }
  29.  
  30.  
  31. /***** background.js *****/
  32. browser.action.onClicked.addListener(async (tab) => {
  33. if (!tab || !tab.url || !tab.title) {
  34. return;
  35. }
  36.  
  37. let clipboardText = "";
  38.  
  39. try {
  40. const clipboardText = await navigator.clipboard.readText();
  41. console.log('Pasted content: ', clipboardText);
  42. } catch (err) {
  43. console.error("Failed to read clipboard:", err);
  44. alert( err );
  45. clipboardText = "";
  46. }
  47.  
  48.  
  49. const pageTitle = tab.title;
  50. const pageUrl = tab.url;
  51.  
  52. // Things URL API:
  53. // https://culturedcode.com/things/support/articles/2803573/
  54. // open "things:///add?title=$TITLE&notes=$URLTD"
  55.  
  56.  
  57. // If processing multiple items, might need to massage like this:
  58. // $urlencoded = urlencode( json_encode($json_src) );
  59. // $urlencoded = str_replace("%22%3A%22", "%22:%22", $urlencoded); # swap : in where %3A appears between quotes
  60. // $urlencoded = str_replace("+", "%20", $urlencoded); # replace '+' with '%20'
  61. // $command = "open things:///json?data=" . $urlencoded;
  62.  
  63.  
  64.  
  65. // Encode values safely for URLs
  66. const encodedTitle = encodeURIComponent(pageTitle);
  67. const encodedUrl = encodeURIComponent(pageUrl);
  68.  
  69. // Construct your target URL
  70. // Example: sending title + URL as query parameters
  71. const targetUrl = `things:///add?title=${encodedTitle}&notes=${encodedUrl}`;
  72.  
  73. async function openAndAutoCloseTab() {
  74. // 1. Open the tab
  75. const tab = await browser.tabs.create({ url: targetUrl, active: false });
  76. const tabId = tab.id;
  77.  
  78. // 2. Listener for when the tab finishes loading
  79. function onUpdated(updatedTabId, changeInfo) {
  80. if (updatedTabId === tabId && changeInfo.status === "complete") {
  81. // Remove listener so it only fires once
  82. browser.tabs.onUpdated.removeListener(onUpdated);
  83.  
  84. // 3. Wait 1 second, then close the tab
  85. setTimeout(() => {
  86. browser.tabs.remove(tabId).catch(() => {
  87. // Tab may already be closed; ignore errors
  88. });
  89. }, 1000);
  90. }
  91. }
  92.  
  93. browser.tabs.onUpdated.addListener(onUpdated);
  94. }
  95.  
  96.  
  97. openAndAutoCloseTab();
  98.  
  99. // // Open the new URL in a new tab
  100. // const ThingsTab = await browser.tabs.create({
  101. // url: targetUrl,
  102. // active: false
  103. // });
  104. // // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/create
  105. // // ThingsTab.close();
  106. });
  107.  
Advertisement
Add Comment
Please, Sign In to add comment