├── src ├── app │ ├── index.ts │ ├── app.component.ts │ ├── login │ │ ├── login.component.scss │ │ ├── login.component.html │ │ └── login.component.ts │ ├── options │ │ ├── options.component.scss │ │ ├── options.component.html │ │ └── options.component.ts │ ├── guard.ts │ ├── icon.service.ts │ ├── util.ts │ ├── interval.pipe.ts │ ├── storage.service.ts │ ├── pinpage │ │ ├── pinpage.component.scss │ │ ├── pinpage.component.html │ │ └── pinpage.component.ts │ ├── background │ │ └── background.component.ts │ └── pinboard.service.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── img │ ├── pinboard_128.png │ ├── pinboard_16.png │ ├── pinboard_24.png │ ├── pinboard_256.png │ ├── pinboard_32.png │ ├── pinboard_48.png │ ├── pinboard_64.png │ ├── pinboard_96.png │ ├── pinboard_idle_128.png │ ├── pinboard_idle_16.png │ ├── pinboard_idle_24.png │ ├── pinboard_idle_256.png │ ├── pinboard_idle_32.png │ ├── pinboard_idle_48.png │ ├── pinboard_idle_64.png │ └── pinboard_idle_96.png ├── polyfills.ts ├── index.html ├── js │ └── content.js ├── main.ts ├── manifest.json └── styles.scss ├── .stylelintrc ├── screenshots ├── pinboard-pin-dark.png ├── pinboard-pin-light.png └── pinboard-pin-settings.png ├── tsconfig.app.json ├── .editorconfig ├── .browserslistrc ├── .gitignore ├── tsconfig.json ├── PRIVACY.md ├── package.json ├── eslint.config.js ├── DEVELOP.md ├── README.md ├── angular.json └── LICENSE /src/app/index.ts: -------------------------------------------------------------------------------- 1 | export * from './app.component'; 2 | -------------------------------------------------------------------------------- /.stylelintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "stylelint-config-standard" 3 | } 4 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/img/pinboard_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_128.png -------------------------------------------------------------------------------- /src/img/pinboard_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_16.png -------------------------------------------------------------------------------- /src/img/pinboard_24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_24.png -------------------------------------------------------------------------------- /src/img/pinboard_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_256.png -------------------------------------------------------------------------------- /src/img/pinboard_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_32.png -------------------------------------------------------------------------------- /src/img/pinboard_48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_48.png -------------------------------------------------------------------------------- /src/img/pinboard_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_64.png -------------------------------------------------------------------------------- /src/img/pinboard_96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_96.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_128.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_16.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_24.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_256.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_32.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_48.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_64.png -------------------------------------------------------------------------------- /src/img/pinboard_idle_96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/src/img/pinboard_idle_96.png -------------------------------------------------------------------------------- /screenshots/pinboard-pin-dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/screenshots/pinboard-pin-dark.png -------------------------------------------------------------------------------- /screenshots/pinboard-pin-light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/screenshots/pinboard-pin-light.png -------------------------------------------------------------------------------- /screenshots/pinboard-pin-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cito/Pinboard-Pin/HEAD/screenshots/pinboard-pin-settings.png -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "files": [ 4 | "src/main.ts", 5 | "src/polyfills.ts" 6 | ], 7 | "include": [ 8 | "src/typings.d.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | // This file includes polyfills needed by Angular and is loaded before 2 | // the app. You can add your own extra polyfills to this file. 3 | 4 | // Zoneless: Zone JS is not required when using provideZonelessChangeDetection. 5 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PinboardPin 6 | 7 | 8 | 9 | 10 | Loading... 11 | 12 | 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | import { RouterOutlet } from '@angular/router'; 3 | 4 | 5 | // Root component of the application 6 | 7 | @Component({ 8 | selector: 'app-root', 9 | template: '', 10 | imports: [RouterOutlet] 11 | }) 12 | export class AppComponent { } 13 | -------------------------------------------------------------------------------- /src/app/login/login.component.scss: -------------------------------------------------------------------------------- 1 | .popup { 2 | width: 400px; 3 | } 4 | 5 | div label { 6 | line-height: 175%; 7 | } 8 | 9 | form div { 10 | display: flex; 11 | flex-direction: row; 12 | align-items: stretch; 13 | input[type="text"] { 14 | flex: 1 0 70px; 15 | margin-right: 8px; 16 | } 17 | 18 | button { 19 | flex: 0 0 80px; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /src/app/options/options.component.scss: -------------------------------------------------------------------------------- 1 | .options { 2 | padding: 8px; 3 | 4 | h1 { 5 | font-size: 24px; 6 | font-weight: normal; 7 | margin: 18px 0 12px; 8 | padding: 0; 9 | } 10 | } 11 | 12 | .popup { 13 | width: 500px; 14 | } 15 | 16 | input[type="checkbox"] { 17 | margin-right: 8px; 18 | } 19 | 20 | input[type="radio"] { 21 | margin-left: 16px; 22 | } 23 | 24 | .popup div button { 25 | float: right; 26 | } 27 | 28 | form div { 29 | display: flex; 30 | align-items: center; 31 | } 32 | -------------------------------------------------------------------------------- /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # For the full list of supported browsers by the Angular framework, please see: 6 | # https://angular.io/guide/browser-support 7 | 8 | # You can see what browsers were selected by your queries by running: 9 | # npx browserslist 10 | 11 | last 1 Firefox version 12 | Firefox ESR 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # cache 4 | .angular/cache 5 | .nx/cache 6 | 7 | # compiled output 8 | /dist 9 | /tmp 10 | /out-tsc 11 | 12 | # dependencies 13 | /node_modules 14 | 15 | # built extensions 16 | *.xpi 17 | *.zip 18 | 19 | # IDEs and editors 20 | .idea/ 21 | .project/ 22 | .classpath 23 | .c9/ 24 | *.launch 25 | .settings/ 26 | *.sublime-workspace 27 | .vscode/ 28 | 29 | # misc 30 | /.sass-cache 31 | /connect.lock 32 | /coverage 33 | /libpeerconnection.log 34 | npm-debug.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | -------------------------------------------------------------------------------- /src/js/content.js: -------------------------------------------------------------------------------- 1 | (function getContent() { 2 | const url = document.location.href; 3 | if (!url || !/^https?:\/\//.test(url)) return null; 4 | let title = document.querySelector('title'); 5 | if (title) title = title.innerText.trim(); 6 | const selection = window.getSelection().toString().trim(); 7 | let meta = document.querySelector('meta[name="description"]'); 8 | const description = meta && (meta.getAttribute('content') || '').trim(); 9 | meta = document.querySelector('meta[name="keywords"]'); 10 | const keywords = meta && (meta.getAttribute('content') || '').trim(); 11 | return {url, title, selection, description, keywords}; 12 | })(); 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "outDir": "./dist/out-tsc", 5 | "esModuleInterop": true, 6 | "sourceMap": true, 7 | "declaration": false, 8 | "moduleResolution": "bundler", 9 | "experimentalDecorators": true, 10 | "typeRoots": [ 11 | "node_modules/@types", 12 | "node_modules/web-ext-types" 13 | ], 14 | "target": "ES2022", 15 | "module": "es2020", 16 | "useDefineForClassFields": false, 17 | "lib": [ 18 | "ES2022", 19 | "DOM" 20 | ] 21 | }, 22 | "angularCompilerOptions": { 23 | "preserveWhitespaces": false, 24 | "strictInjectionParameters": true, 25 | "strictInputAccessModifiers": true, 26 | "strictStandAlone": true, 27 | "strictTemplates": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/app/guard.ts: -------------------------------------------------------------------------------- 1 | import { inject } from "@angular/core"; 2 | import { Router, ActivatedRouteSnapshot } from "@angular/router"; 3 | 4 | import { map } from "rxjs/operators"; 5 | 6 | import { PinboardService } from "./pinboard.service"; 7 | 8 | // Guard for the index route of this extension 9 | 10 | export const guard = (route: ActivatedRouteSnapshot) => { 11 | const pinboard = inject(PinboardService); 12 | const router = inject(Router); 13 | 14 | const page: string | undefined = route.queryParams["page"] as 15 | | string 16 | | undefined; 17 | 18 | if (!page || page === "popup") { 19 | return pinboard.needToken.pipe( 20 | map((needed) => { 21 | if (!needed) { 22 | return true; 23 | } 24 | void router.navigate(["/login"]); 25 | return false; 26 | }) 27 | ); 28 | } 29 | 30 | void router.navigate(["/" + page]); 31 | return false; 32 | }; 33 | -------------------------------------------------------------------------------- /src/app/icon.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from "@angular/core"; 2 | 3 | const defaultIcon = { 4 | "16": "/img/pinboard_idle_16.png", 5 | "24": "/img/pinboard_idle_24.png", 6 | "32": "/img/pinboard_idle_32.png", 7 | "48": "/img/pinboard_idle_48.png", 8 | "64": "/img/pinboard_idle_64.png", 9 | "96": "/img/pinboard_idle_96.png", 10 | "128": "/img/pinboard_idle_128.png", 11 | "256": "/img/pinboard_idle_256.png", 12 | }; 13 | 14 | const savedIcon = { 15 | "16": "/img/pinboard_16.png", 16 | "24": "/img/pinboard_24.png", 17 | "32": "/img/pinboard_32.png", 18 | "48": "/img/pinboard_48.png", 19 | "64": "/img/pinboard_64.png", 20 | "96": "/img/pinboard_96.png", 21 | "128": "/img/pinboard_128.png", 22 | "256": "/img/pinboard_256.png", 23 | }; 24 | 25 | // Service for setting the browser action icon 26 | 27 | @Injectable({ providedIn: "root" }) 28 | export class IconService { 29 | constructor() {} 30 | 31 | // set the icon for saved or unsaved for the given tab 32 | setIcon(tabId: number, saved: boolean) { 33 | void browser.browserAction.setIcon({ 34 | tabId: tabId, 35 | path: saved ? savedIcon : defaultIcon, 36 | }); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/app/util.ts: -------------------------------------------------------------------------------- 1 | // Utility functions for Pinboard Pin 2 | 3 | // convert an unknown error to a string message 4 | export function errorMessage(error: unknown): string { 5 | if (typeof error === "string") { 6 | return error; 7 | } 8 | if (error instanceof Error) { 9 | return error.message; 10 | } 11 | if (error && typeof error === "object") { 12 | try { 13 | const json: string = JSON.stringify(error); 14 | if (typeof json === "string") { 15 | return json; 16 | } 17 | } catch { 18 | /* ignore */ 19 | } 20 | } 21 | if ( 22 | typeof error === "number" || 23 | typeof error === "boolean" || 24 | typeof error === "bigint" || 25 | typeof error === "symbol" 26 | ) { 27 | return String(error); 28 | } 29 | return "Unknown error"; 30 | } 31 | 32 | // log an unknown error to the console and return its string message 33 | export function logError(error: unknown, context?: unknown): string { 34 | const base = errorMessage(error); 35 | const prefix = context !== undefined ? `${errorMessage(context)}: ` : ""; 36 | const message = `${prefix}${base}`; 37 | console.error(message); 38 | return message; 39 | } 40 | -------------------------------------------------------------------------------- /src/app/login/login.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |

Log in to Pinboard

4 | 5 |

You must log in to Pinboard first using the API token 6 | that you can find on your 7 | Pinboard password page.

8 | 9 |
10 |
11 | @if (!checking) { 12 | 20 | } 21 | @if (checking) { 22 | 24 | } 25 |
26 |
27 | 29 | 32 |
33 |
34 | 35 |
-------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode, provideZonelessChangeDetection } from '@angular/core'; 2 | import { bootstrapApplication } from '@angular/platform-browser'; 3 | import { provideRouter } from '@angular/router'; 4 | import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; 5 | 6 | import { AppComponent } from './app/app.component'; 7 | import { LoginComponent } from './app/login/login.component'; 8 | import { PinPageComponent } from './app/pinpage/pinpage.component'; 9 | import { OptionsComponent } from './app/options/options.component'; 10 | import { BackgroundComponent } from './app/background/background.component'; 11 | import { guard } from './app/guard'; 12 | import { environment } from './environments/environment'; 13 | 14 | if (environment.production) { 15 | enableProdMode(); 16 | } 17 | 18 | const appRoutes = [ 19 | { path: 'login', component: LoginComponent }, 20 | { path: 'options', component: OptionsComponent }, 21 | { path: 'background', component: BackgroundComponent }, 22 | { path: '**', component: PinPageComponent, canActivate: [guard] } 23 | ]; 24 | 25 | bootstrapApplication(AppComponent, { 26 | providers: [ 27 | provideZonelessChangeDetection(), 28 | provideRouter(appRoutes), 29 | provideHttpClient(withInterceptorsFromDi()) 30 | ] 31 | }).catch(error => console.error(error)); 32 | -------------------------------------------------------------------------------- /PRIVACY.md: -------------------------------------------------------------------------------- 1 | Privacy Policy for the Pinboard-Pin Add-On 2 | ========================================== 3 | 4 | This add-on communicates with the [Pinboard](https://pinboard.in/) service using the [Pinboard API](https://pinboard.in/api) for the purpose of storing and updating bookmarks in your personal Pinboard account. 5 | 6 | In order to access your personal Pinboard account, your personal Pinboard API token is requested by the add-on and cached locally until you log out using the add-on. Your Pinboard password is not requested, accessed or stored by the add-on. 7 | 8 | In order to provide suggestions for tagging bookmarks, the add-on retrieves your tags stored in your personal Pinboard account using the Pinboard API and caches these tags locally. 9 | 10 | The data sent to the Pinboard service are the URL of the bookmarked link, its title, description, tags, and privacy and read status on Pinboard. 11 | 12 | If you activate the option "always check and show whether pages have been bookmarked" (which is deactivated by default), then the add-on checks for every tab whether it has been already bookmarked. For this purpose, it sends the URL of the tab to the Pinboard service using the Pinboard API. 13 | 14 | Please also read the [Pinboard privacy policy](https://pinboard.in/privacy/) for information on the privacy police of the Pinboard service itself. 15 | 16 | The author of this add-on is not affiliated with the Pinboard service in any way and does not take any responsibility for how the service handles your bookmarks. 17 | -------------------------------------------------------------------------------- /src/app/interval.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from "@angular/core"; 2 | 3 | const msPerSecond = 1000; 4 | const msPerMinute = msPerSecond * 60; 5 | const msPerHour = msPerMinute * 60; 6 | const msPerDay = msPerHour * 24; 7 | const msPerMonth = msPerDay * 30; 8 | const msPerYear = msPerDay * 365; 9 | 10 | const intervalUnits = ["second", "minute", "hour", "day", "month", "year"]; 11 | const intervalParts = [ 12 | msPerSecond, 13 | msPerMinute, 14 | msPerHour, 15 | msPerDay, 16 | msPerMonth, 17 | msPerYear, 18 | ]; 19 | 20 | // Pipe for showing time intervals 21 | 22 | @Pipe({ 23 | name: "ago", 24 | }) 25 | export class AgoPipe implements PipeTransform { 26 | // show date as passed time in simple human readable form 27 | transform(value: Date | string): string { 28 | if (!value) { 29 | return "some time ago"; 30 | } 31 | 32 | const time: number = 33 | typeof value === "string" ? Date.parse(value) : value.getTime(); 34 | const elapsed = Date.now() - time; 35 | 36 | if (elapsed < 0) { 37 | return "some time ago"; 38 | } 39 | if (elapsed < msPerSecond) { 40 | return "just now"; 41 | } 42 | 43 | for (let i = 0; i < 6; ++i) { 44 | if (i > 4 || elapsed < intervalParts[i + 1]) { 45 | const n = Math.round(elapsed / intervalParts[i]); 46 | let unit = intervalUnits[i]; 47 | if (n !== 1) { 48 | unit += "s"; 49 | } 50 | return n + " " + unit + " ago"; 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pinboard-pin", 3 | "version": "0.4.4", 4 | "license": "MIT", 5 | "type": "module", 6 | "scripts": { 7 | "build": "ng build", 8 | "build:prod": "ng build --configuration production --source-map=false --output-hashing=none", 9 | "build:ext": "web-ext build --source-dir=dist/browser --artifacts-dir=. --overwrite-dest", 10 | "build:zip": "npm run build:prod && npm run build:ext", 11 | "lint": "eslint . --ext .ts,.html", 12 | "lint:ext": "npm run build:prod && web-ext lint --source-dir=dist/browser", 13 | "test": "web-ext run --source-dir=dist/browser --firefox='firefox'" 14 | }, 15 | "private": true, 16 | "dependencies": { 17 | "@angular/common": "^21.0.6", 18 | "@angular/compiler": "^21.0.6", 19 | "@angular/core": "^21.0.6", 20 | "@angular/forms": "^21.0.6", 21 | "@angular/platform-browser": "^21.0.6", 22 | "@angular/platform-browser-dynamic": "^21.0.6", 23 | "@angular/router": "^21.0.6", 24 | "rxjs": "^7.8.2", 25 | "tslib": "^2.8.1" 26 | }, 27 | "devDependencies": { 28 | "@angular-devkit/build-angular": "^21.0.4", 29 | "@angular/cli": "^21.0.4", 30 | "@angular/compiler-cli": "^21.0.6", 31 | "@angular/language-service": "^21.0.6", 32 | "@angular-eslint/builder": "^21.1.0", 33 | "@angular-eslint/eslint-plugin": "^21.1.0", 34 | "@angular-eslint/eslint-plugin-template": "^21.1.0", 35 | "@angular-eslint/schematics": "^21.1.0", 36 | "@angular-eslint/template-parser": "^21.1.0", 37 | "@types/node": "^25.0.3", 38 | "@typescript-eslint/eslint-plugin": "^8.50.0", 39 | "@typescript-eslint/parser": "^8.50.0", 40 | "@types/firefox-webext-browser": "^143.0.0", 41 | "eslint": "^9.39.2", 42 | "typescript": "^5.4.5", 43 | "web-ext": "^9.2.0" 44 | } 45 | } -------------------------------------------------------------------------------- /src/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 2, 3 | 4 | "name": "Pinboard Pin", 5 | "version": "0.4.4", 6 | "description": "A web extension for pinning pages on Pinboard (pinboard.in) with Firefox.", 7 | "homepage_url": "https://github.com/Cito/Pinboard-Pin", 8 | 9 | "options_ui": { 10 | "page": "/index.html?page=options" 11 | }, 12 | 13 | "icons": { 14 | "16": "/img/pinboard_16.png", 15 | "24": "/img/pinboard_24.png", 16 | "32": "/img/pinboard_32.png", 17 | "48": "/img/pinboard_48.png", 18 | "64": "/img/pinboard_64.png", 19 | "96": "/img/pinboard_96.png", 20 | "128": "/img/pinboard_128.png", 21 | "256": "/img/pinboard_256.png" 22 | }, 23 | 24 | "background": { 25 | "page": "/index.html?page=background" 26 | }, 27 | 28 | "browser_action": { 29 | "default_title": "Pinboard Pin", 30 | "default_icon": { 31 | "16": "/img/pinboard_idle_16.png", 32 | "24": "/img/pinboard_idle_24.png", 33 | "32": "/img/pinboard_idle_32.png", 34 | "48": "/img/pinboard_idle_48.png", 35 | "64": "/img/pinboard_idle_64.png", 36 | "96": "/img/pinboard_idle_96.png", 37 | "128": "/img/pinboard_idle_128.png", 38 | "256": "/img/pinboard_idle_256.png" 39 | }, 40 | "default_popup": "/index.html" 41 | }, 42 | 43 | "commands": { 44 | "_execute_browser_action": { 45 | "suggested_key": { 46 | "default": "Alt+P" 47 | }, 48 | "description": "Save page to Pinboard" 49 | } 50 | }, 51 | 52 | "permissions": ["storage", "tabs", "contextMenus", "*://*/*"], 53 | 54 | "browser_specific_settings": { 55 | "gecko": { 56 | "id": "{981c6263-af64-47c6-8c6d-c3289444fdf9}", 57 | "data_collection_permissions": { 58 | "required": [ 59 | "browsingActivity", 60 | "websiteContent" 61 | ] 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | // Flat ESLint config for Angular + TypeScript + templates (ESLint v9) 2 | import pluginJs from '@eslint/js'; 3 | import tsParser from '@typescript-eslint/parser'; 4 | import tsPlugin from '@typescript-eslint/eslint-plugin'; 5 | import angular from '@angular-eslint/eslint-plugin'; 6 | import angularTemplate from '@angular-eslint/eslint-plugin-template'; 7 | import angularTemplateParser from '@angular-eslint/template-parser'; 8 | import globals from 'globals'; 9 | 10 | export default [ 11 | // Ignore build outputs and vendor dirs 12 | { ignores: ['dist/**', 'node_modules/**', '*.zip'] }, 13 | 14 | pluginJs.configs.recommended, 15 | 16 | { 17 | files: ['**/*.ts'], 18 | languageOptions: { 19 | parser: tsParser, 20 | parserOptions: { 21 | ecmaVersion: 'latest', 22 | sourceType: 'module', 23 | // Enable type-aware rules by using the project service 24 | projectService: true 25 | }, 26 | globals: { 27 | ...globals.browser, 28 | browser: 'readonly' 29 | } 30 | }, 31 | plugins: { 32 | '@angular-eslint': angular, 33 | '@typescript-eslint': tsPlugin 34 | }, 35 | rules: { 36 | // Type-aware TS recommendations 37 | ...tsPlugin.configs['recommended-type-checked'].rules, 38 | '@typescript-eslint/no-explicit-any': 'off', 39 | '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 40 | 'no-prototype-builtins': 'off', 41 | 'no-case-declarations': 'off' 42 | } 43 | }, 44 | 45 | { 46 | files: ['**/*.js'], 47 | languageOptions: { 48 | globals: { 49 | ...globals.browser, 50 | browser: 'readonly' 51 | } 52 | } 53 | }, 54 | 55 | { 56 | files: ['**/*.html'], 57 | languageOptions: { 58 | parser: angularTemplateParser 59 | }, 60 | plugins: { 61 | '@angular-eslint/template': angularTemplate 62 | }, 63 | rules: { 64 | ...angularTemplate.configs.recommended.rules 65 | } 66 | } 67 | ]; 68 | -------------------------------------------------------------------------------- /src/app/login/login.component.ts: -------------------------------------------------------------------------------- 1 | // this component is the login dialog displayed in the popup 2 | 3 | import { Component, OnInit, ChangeDetectorRef } from "@angular/core"; 4 | import { CommonModule } from "@angular/common"; 5 | import { FormsModule, NgForm } from "@angular/forms"; 6 | import { Router } from "@angular/router"; 7 | import { finalize } from "rxjs/operators"; 8 | 9 | import { passwordPage, PinboardService } from "../pinboard.service"; 10 | import { Options, StorageService } from "../storage.service"; 11 | import { logError } from "../util"; 12 | 13 | export interface Login { 14 | token: string; 15 | } 16 | 17 | // Log in form 18 | 19 | @Component({ 20 | selector: "app-login", 21 | templateUrl: "./login.component.html", 22 | styleUrls: ["./login.component.scss"], 23 | imports: [CommonModule, FormsModule], 24 | }) 25 | export class LoginComponent implements OnInit { 26 | checking = false; 27 | error = false; 28 | 29 | theme = "light"; // color scheme of the page 30 | 31 | constructor( 32 | private pinboard: PinboardService, 33 | private storage: StorageService, 34 | private router: Router, 35 | private cdr: ChangeDetectorRef 36 | ) {} 37 | 38 | ngOnInit() { 39 | this.storage.getOptions().subscribe((options) => { 40 | this.setTheme(options); 41 | this.cdr.detectChanges(); 42 | }); 43 | } 44 | 45 | setTheme(options: Options) { 46 | this.theme = 47 | options.dark === true || 48 | (options.dark !== false && 49 | window.matchMedia("(prefers-color-scheme: dark)").matches) 50 | ? "dark" 51 | : "light"; 52 | } 53 | 54 | openPasswordPage() { 55 | void browser.windows.create({ url: passwordPage }); 56 | return false; 57 | } 58 | 59 | // submit form (store token) 60 | submit(form: NgForm) { 61 | if (!form.valid) { 62 | return false; 63 | } 64 | let token: string = (form.value as Record).token as string; 65 | if (!token) { 66 | return false; 67 | } 68 | token = token.trim(); 69 | if (!token) { 70 | return false; 71 | } 72 | this.checking = true; 73 | this.pinboard 74 | .setToken(token) 75 | .pipe(finalize(() => this.cdr.detectChanges())) 76 | .subscribe({ 77 | next: (ok) => { 78 | this.error = !ok; 79 | if (ok) { 80 | this.continue(); 81 | } else { 82 | this.checking = false; 83 | } 84 | }, 85 | error: (error: unknown) => { 86 | this.error = true; 87 | logError(error); 88 | this.checking = false; 89 | }, 90 | }); 91 | return false; 92 | } 93 | 94 | continue() { 95 | void this.router.navigate(["/popup"]); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /DEVELOP.md: -------------------------------------------------------------------------------- 1 | Development Notes for Pinboard Pin 2 | ================================== 3 | 4 | This is an add-on for pinning pages on [Pinboard](https://pinboard.in) in the web browser, based on the [WebExtensions](https://developer.mozilla.org/de/Add-ons/WebExtensions) system. This web extension has been developed for and tested with Firefox 80 for Windows and Linux. Since WebExtensions was created as a cross-browser standard, it should be portable to other platforms and web browsers like Chrome, Opera or Edge with only few adaptations. 5 | 6 | The web extension has been created using [Angular](https://angular.io/) and the [Angular CLI](https://github.com/angular/angular-cli). 7 | 8 | Installation 9 | ------------ 10 | 11 | Install the application for development: 12 | 13 | npm install 14 | 15 | Building and testing 16 | -------------------- 17 | 18 | Build extension for development: 19 | 20 | npm run build 21 | 22 | Test extension with Firefox: 23 | 24 | npm run test 25 | 26 | Build unsigned extension for production: 27 | 28 | npm run build:prod 29 | 30 | Package unsigned extension as zip file: 31 | 32 | npm run build:zip 33 | 34 | Known issues 35 | ------------ 36 | 37 | * Due to limitations in the Web-Ext API, the keyboard shortcut (Alt+P) cannot be changed or deactivated (at least I don't see a simple way how to do this - let me know if I'm overlooking something). 38 | 39 | * Unfortunately, storing tabsets is not supported by the Pinboard API. Therefore you need to be logged in to Pinboard in order use this feature. This works the same as in the official add-on. 40 | 41 | * If you get errors when requesting data from the Pinboard API via https, these could be caused by the Privacy Badger extension. Make sure that the domain pinboard.in is fully enabled in Privacy Badger or whitelist it. 42 | 43 | Future development 44 | ------------------ 45 | 46 | * Use v2 of the Pinboard API when this is available with all the necessary functionality. 47 | 48 | * Require only those permissions that are needed for the selected options (issue #8). 49 | 50 | * The Pinboard API seems to have the "popular" and "recommended" categories interchanged in the "suggest" method for tags. "Popular" are actually those taken from our own tags, contrary to what the Pinboard API docs say. This has already been reported to the Pinboard support. If they will changed this behavior, we need to adapt our code that currently swaps these categories. 51 | 52 | * Ask Pinboard author to provide a method for storing tabsets and support it in the add-on. 53 | 54 | * The PinPageComponent is much too big, and should be refactored to use child components, and objects instead of individual properties for the form fields, similar to the OptionsComponent. 55 | 56 | * Make this extension also work with Chrome (use the `chrome.*` API instead of the `browser.*` API provided by Firefox). 57 | -------------------------------------------------------------------------------- /src/app/storage.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from "@angular/core"; 2 | 3 | import { Observable, from } from "rxjs"; 4 | import { map } from "rxjs/operators"; 5 | 6 | export interface Options { 7 | ping: boolean; // check whether pages are bookmarked 8 | unshared: boolean; // add as private by default 9 | toread: boolean; // add as to read by default 10 | meta: boolean; // parse meta elements 11 | selection: boolean; // use selected text 12 | blockquote: boolean; // wrap as block quote 13 | alpha: boolean; // sort alphabetically 14 | popular: boolean; // show popular tags 15 | menu: boolean; // add entry to context menu 16 | dark: boolean | null; // tri-state: on/off/auto 17 | } 18 | 19 | const defaultOptions: Options = { 20 | ping: false, 21 | unshared: false, 22 | toread: false, 23 | meta: true, 24 | selection: true, 25 | blockquote: false, 26 | alpha: false, 27 | popular: true, 28 | menu: false, 29 | dark: null, 30 | }; 31 | 32 | // Wrapper around the browser local storage for the web extension, 33 | // using Observables instead of Promises for flexibility and consistency. 34 | 35 | @Injectable({ providedIn: "root" }) 36 | export class StorageService { 37 | private storage = browser.storage.local; // needs "storage" permission 38 | 39 | private readonly info: Record; 40 | 41 | constructor() { 42 | this.info = {}; // for various info shared via this service 43 | } 44 | 45 | // get keys from local storage as an Observable 46 | // when only one key is requested, only its value is returned 47 | get(keys: string | string[] | null): Observable { 48 | return from(this.storage.get(keys)).pipe( 49 | map((res: Record) => 50 | typeof keys === "string" ? res[keys] : res 51 | ) 52 | ); 53 | } 54 | 55 | // set keys in local storage as an Observable 56 | set(keys: Record): Observable { 57 | return from(this.storage.set(keys)); 58 | } 59 | 60 | // remove keys in local storage as an Observable 61 | remove(keys: string | string[]): Observable { 62 | return from(this.storage.remove(keys)); 63 | } 64 | 65 | // retrieve options from local storage 66 | getOptions(): Observable { 67 | return this.get("options").pipe( 68 | map((options: Record | null | undefined) => { 69 | const opts: Partial = {}; 70 | Object.keys(defaultOptions).forEach((key) => { 71 | const val = options ? options[key] : undefined; 72 | opts[key as keyof Options] = 73 | val === undefined 74 | ? defaultOptions[key as keyof Options] 75 | : (val as never); 76 | }); 77 | return opts as Options; 78 | }) 79 | ); 80 | } 81 | 82 | // store options in local storage 83 | setOptions(options: Options): Observable { 84 | return this.set({ options: options }); 85 | } 86 | 87 | // set a volatile info for users of this service 88 | setInfo(name: string, value: any): void { 89 | this.info[name] = value; 90 | } 91 | 92 | // get a volatile info that was previously set 93 | getInfo(name: string): any { 94 | return this.info[name]; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/app/pinpage/pinpage.component.scss: -------------------------------------------------------------------------------- 1 | .popup { 2 | width: 720px; 3 | } 4 | 5 | .error { 6 | color: var(--color-error); 7 | text-align: center; 8 | font-weight: bold; 9 | } 10 | 11 | div.buttons { 12 | display: flex; 13 | justify-content: space-between; 14 | margin-top: 8px; 15 | margin-bottom: 16px; 16 | 17 | button { 18 | font-size: 11px; 19 | max-width: 70px; 20 | } 21 | } 22 | 23 | form { 24 | div { 25 | display: flex; 26 | align-items: baseline; 27 | margin-top: 8px; 28 | 29 | label { 30 | flex: 0 0 100px; 31 | margin-right: 8px; 32 | text-align: right; 33 | font-weight: bold; 34 | } 35 | 36 | input[type="text"], 37 | input[type="url"], 38 | textarea { 39 | flex: 1 0 70px; 40 | } 41 | } 42 | 43 | div.info { 44 | margin-bottom: 8px; 45 | } 46 | 47 | div.submit { 48 | margin-top: 16px; 49 | display: flex; 50 | align-items: center; 51 | 52 | label { 53 | text-align: left; 54 | width: auto; 55 | 56 | + button { 57 | margin-left: auto; 58 | } 59 | } 60 | 61 | button { 62 | margin-left: 8px; 63 | width: 120px; 64 | font-size: 14px; 65 | padding: 8px; 66 | align-self: stretch; 67 | } 68 | } 69 | 70 | div.completions { 71 | display: block; 72 | position: relative; 73 | margin: 0; 74 | padding: 0; 75 | height: 0; 76 | } 77 | } 78 | 79 | span.suggested { 80 | display: flex; 81 | align-items: baseline; 82 | flex-wrap: wrap; 83 | a { 84 | color: hsl(var(--hue), 40%, 33%); 85 | margin-right: 8px; 86 | text-decoration: none; 87 | 88 | &.added { 89 | color: hsl(var(--hue), 5%, 60%); 90 | } 91 | 92 | &:last-child { 93 | font-weight: bold; 94 | } 95 | 96 | &:nth-last-child(2) { 97 | margin-right: 12px; 98 | } 99 | } 100 | } 101 | 102 | input[type="checkbox"] { 103 | flex: 0 0 30px; 104 | width: 30px; 105 | height: 30px; 106 | margin-right: 12px; 107 | margin-bottom: 4px; 108 | 109 | &::before { 110 | font-size: 28px; 111 | margin-left: 3px; 112 | } 113 | } 114 | 115 | .completions { 116 | ul { 117 | position: absolute; 118 | list-style-type: none; 119 | bottom: -2px; 120 | left: 110px; 121 | width: 300px; 122 | max-height: 250px; 123 | overflow-y: scroll; 124 | margin: 0; 125 | padding: 0; 126 | background-color: var(--color-bg-input); 127 | border: 1px solid hsl(var(--hue-2), 15%, 67%); 128 | box-shadow: 4px 4px 10px hsl(var(--hue), 15%, 67%); 129 | color: hsl(var(--hue), 40%, 33%); 130 | z-index: 9; 131 | } 132 | 133 | li { 134 | padding: 4px; 135 | } 136 | 137 | li.selected { 138 | background-color: hsl(var(--hue), 33%, 40%); 139 | color: var(--color-bg-input); 140 | } 141 | } 142 | 143 | main.dark { 144 | span.suggested { 145 | a { 146 | color: hsl(var(--hue), 60%, 67%); 147 | 148 | &.added { 149 | color: hsl(var(--hue), 5%, 40%); 150 | } 151 | } 152 | } 153 | 154 | .completions { 155 | ul { 156 | border: 1px solid hsl(var(--hue-2), 15%, 25%); 157 | box-shadow: 4px 4px 10px hsl(var(--hue), 15%, 3%); 158 | color: hsl(var(--hue), 33%, 86%); 159 | } 160 | 161 | li.selected { 162 | background-color: hsl(var(--hue), 40%, 80%); 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/app/options/options.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | @if (page === 'popup') { 4 |
5 | 6 |
7 | } 8 | 9 |

{{ page === 'popup'? 'Pinboard Pin Options' : 'Options' }}

10 | 11 |
12 |
13 | 15 | 16 |
17 |
18 | 20 | 21 |
22 |
23 | 25 | 26 |
27 |
28 | 30 | 31 |
32 |
33 | 35 | 36 |
37 |
38 | 40 | 41 |
42 |
43 | 45 | 46 |
47 |
48 | 50 | 51 |
52 |
53 | 55 | 56 |
57 |
58 | 59 | 61 | 62 | 64 | 65 | 67 | 68 |
69 |
70 | Keyboard shortcut for saving pages to Pinboard Pin: 71 | {{shortcut}} 72 |
73 |
74 | 75 |
-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Pinboard Pin Web Extension for Firefox 2 | -------------------------------------- 3 | 4 | Pinboard Pin is a web extension for pinning pages on [Pinboard](https://pinboard.in) with Firefox. 5 | 6 | Pinboard is a fast, no-nonsense bookmarking site for people who value privacy and speed. Note that while the Pinboard Pin extension is provided by its author as free and open source software, Pinboard is a separate, paid service and you need to sign up for an account in order to use it. The author of Pinboard Pin is not affiliated with the provider of the Pinboard service. For any questions regarding the Pinboard service itself, please contact the Pinboard support. 7 | 8 | This extension pretty much replicates the functionality of the [Pinboard Plus](https://github.com/clvrobj/Pinboard-Plus) extension that has been created for the Chrome browser, but it has been rewritten from scratch to support Firefox using the [Angular](https://angular.io/) framework instead of a mix of the older [AngularJS](https://angularjs.org/), [jQuery](https://jquery.com/) and [Underscore](http://underscorejs.org/) libraries used by Pinboard Plus. This rewrite was also intended as a proof-of-concept that Angular is a viable platform for building web extensions. 9 | 10 | The current version has been tested with Firefox 80 for Windows and Linux. 11 | 12 | Features 13 | -------- 14 | 15 | * Icon that changes its color to show whether the current page has been bookmarked in Pinboard. 16 | * Save a new bookmark for the current page in Pinboard, adding description and tags in a popup dialog. 17 | * Update and delete the bookmark for the current page in Pinboard from the popup. 18 | * Automatic creation of a description from a selected text on the page or using an existing meta tag on the page. 19 | * Enter or update tags using automatic suggestions in the popup dialog. 20 | * Use a keyboard shortcut (Alt + P) to open the popup dialog. 21 | * Use the context menu to open the popup dialog for the current page or to quickly save any link on the page. 22 | * Configurable dark mode for the popup dialog. 23 | 24 | Installation 25 | ------------ 26 | 27 | You can install the extension using the [Mozilla Add-ons](https://addons.mozilla.org/de/firefox/addon/pinboard-pin/) page. 28 | 29 | Notes 30 | ----- 31 | 32 | Note that you must explicitly enable the option for displaying whether the page has already been saved on Pinboard, because this will send the URL of the page to the Pinboard server for every page. Therefore this feature is also automatically disabled in "incognito mode". 33 | 34 | Also note that if you are using the Privacy Badger extension and Pinboard Pin shows an error when accessing Pinboard, make sure that the domain pinboard.in is not blocked in the filter settings of Privacy Badger, or simply put in on the whitelist of Privacy Badger. 35 | 36 | Development 37 | ----------- 38 | 39 | * Follow the instructions outlined in [DEVELOP.md](https://github.com/cito/Pinboard-Pin/blob/master/DEVELOP.md) if you want to build this extension on your own or run it in development mode. 40 | * Read my blog post [Web Extensions made with Angular](https://cito.github.io/blog/web-ext-with-angular/) to learn more about how and why this extension has been created. 41 | * Contact the author, open GitHub issues or send in pull requests to contribute. 42 | 43 | Author 44 | ------ 45 | 46 | * [Christoph Zwerschke](https://github.com/cito) 47 | 48 | Some Screenshots 49 | ---------------- 50 | 51 | Saving a page with Pinboard Pin: 52 | 53 | ![Saving a page - light mode](screenshots/pinboard-pin-light.png?raw=true) 54 | 55 | Changing the Pinboard Pin settings: 56 | 57 | ![Settings - light mode](screenshots/pinboard-pin-settings.png?raw=true) 58 | 59 | Pinboard Pin also supports dark mode: 60 | 61 | ![Saving a page - dark mode](screenshots/pinboard-pin-dark.png?raw=true) 62 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | body { 4 | font-family: sans-serif; 5 | font-size: 14px; 6 | padding: 0; 7 | margin: 0; 8 | } 9 | 10 | main { 11 | --hue: 220; 12 | --hue-2: 100; 13 | --color-lines: hsl(var(--hue-2), 33%, 40%); 14 | --color-focus: hsl(var(--hue-2), 40%, 67%); 15 | --color-bg: hsl(var(--hue), 5%, 95%); 16 | --color-fg: hsl(var(--hue), 5%, 5%); 17 | --color-bg-input: hsl(var(--hue), 10%, 90%); 18 | --color-fg-input: hsl(var(--hue), 10%, 10%); 19 | --color-error: hsl(0, 67%, 50%); 20 | --color-info: hsl(120, 33%, 37%); 21 | --color-note: hsl(240, 10%, 33%); 22 | --color-link: hsl(250, 67%, 33%); 23 | 24 | color: var(--color-fg); 25 | background-color: var(--color-bg); 26 | } 27 | 28 | .popup { 29 | padding: 8px; 30 | } 31 | 32 | .popup h1 { 33 | font-size: 24px; 34 | line-height: 36px; 35 | background: url("/img/pinboard_32.png") no-repeat; 36 | padding: 2px 48px; 37 | margin: 0 0 4px; 38 | border-bottom: 1px solid var(--color-lines); 39 | } 40 | 41 | input[type="text"], 42 | input[type="url"], 43 | textarea { 44 | font-family: sans-serif; 45 | font-size: 14px; 46 | padding: 8px; 47 | border-radius: 4px; 48 | border: 1px solid var(--color-lines); 49 | background-color: var(--color-bg-input); 50 | color: var(--color-fg-input); 51 | width: 42em; 52 | } 53 | 54 | button { 55 | border: none; 56 | border-bottom: 2px solid hsl(var(--hue), 20%, 15%); 57 | border-radius: 4px; 58 | background: linear-gradient(to bottom, hsl(var(--hue), 40%, 33%) 0%, hsl(var(--hue), 30%, 25%) 100%); 59 | color: hsl(var(--hue), 25%, 95%); 60 | padding: 6px; 61 | font-weight: 600; 62 | font-size: 12px; 63 | box-shadow: 0 2px 0 0 rgba(0, 0, 0, 0.1); 64 | } 65 | 66 | button:hover { 67 | --hue: var(--hue-2); 68 | } 69 | 70 | button[type="submit"] { 71 | border-width: 2px; 72 | } 73 | 74 | button:disabled, 75 | button:disabled:hover { 76 | opacity: 0.4; 77 | } 78 | 79 | input[type="checkbox"], 80 | input[type="radio"] { 81 | -moz-appearance: none; 82 | border-radius: 4px; 83 | border: 1px solid var(--color-lines); 84 | background-color: var(--color-bg-input); 85 | width: 16px; 86 | height: 16px; 87 | 88 | &::before { 89 | content: "\2713"; 90 | text-align: center; 91 | color: var(--color-bg-input); 92 | font-size: 14px; 93 | line-height: 1; 94 | float: left; 95 | margin-left: 1.5px; 96 | } 97 | 98 | &:checked { 99 | background-color: hsl(var(--hue), 40%, 33%); 100 | } 101 | } 102 | 103 | input[type="radio"] { 104 | border-radius: 8px; 105 | 106 | &::before { 107 | content: "\2b24"; 108 | font-size: 12px; 109 | } 110 | } 111 | 112 | input[type="text"], 113 | input[type="url"], 114 | input[type="checkbox"], 115 | input[type="radio"], 116 | textarea, 117 | button { 118 | &:focus { 119 | outline: solid 2px var(--color-focus); 120 | -moz-outline-radius: 6px; 121 | } 122 | } 123 | 124 | input[type="radio"]:focus { 125 | -moz-outline-radius: 9px; 126 | } 127 | 128 | span.error { 129 | color: var(--color-error); 130 | } 131 | 132 | span.info { 133 | color: var(--color-info); 134 | } 135 | 136 | span.note { 137 | color: var(--color-note); 138 | } 139 | 140 | a { 141 | color: var(--color-link); 142 | text-decoration: none; 143 | } 144 | 145 | a:hover { 146 | text-decoration: underline; 147 | } 148 | 149 | main.dark { 150 | --color-bg: hsl(var(--hue), 25%, 7%); 151 | --color-fg: hsl(var(--hue), 12%, 93%); 152 | --color-bg-input: hsl(var(--hue), 25%, 15%); 153 | --color-fg-input: hsl(var(--hue), 12%, 85%); 154 | --color-focus: hsl(var(--hue-2), 40%, 50%); 155 | --color-error: hsl(0, 75%, 57%); 156 | --color-info: hsl(120, 67%, 40%); 157 | --color-note: hsl(240, 10%, 60%); 158 | --color-link: hsl(250, 33%, 67%); 159 | 160 | input[type="checkbox"], 161 | input[type="radio"] { 162 | &:checked { 163 | background-color: hsl(var(--hue), 50%, 85%); 164 | } 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "pinboard-pin": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | }, 12 | "@schematics/angular:application": { 13 | "strict": true 14 | } 15 | }, 16 | "root": "", 17 | "sourceRoot": "src", 18 | "prefix": "app", 19 | "architect": { 20 | "build": { 21 | "builder": "@angular-devkit/build-angular:application", 22 | "options": { 23 | "outputPath": { 24 | "base": "dist" 25 | }, 26 | "index": "src/index.html", 27 | 28 | "tsConfig": "tsconfig.app.json", 29 | "inlineStyleLanguage": "scss", 30 | "assets": [ 31 | "src/img", 32 | "src/js", 33 | "src/manifest.json" 34 | ], 35 | "styles": [ 36 | "src/styles.scss" 37 | ], 38 | "scripts": [], 39 | "extractLicenses": false, 40 | "sourceMap": true, 41 | "optimization": false, 42 | "namedChunks": true, 43 | "browser": "src/main.ts" 44 | }, 45 | "configurations": { 46 | "production": { 47 | "budgets": [ 48 | { 49 | "type": "initial", 50 | "maximumWarning": "500kb", 51 | "maximumError": "1mb" 52 | }, 53 | { 54 | "type": "anyComponentStyle", 55 | "maximumWarning": "3kb", 56 | "maximumError": "5kb" 57 | } 58 | ], 59 | "optimization": { 60 | "scripts": true, 61 | "styles": { 62 | "minify": true, 63 | "inlineCritical": false 64 | }, 65 | "fonts": true 66 | }, 67 | "outputHashing": "all", 68 | "sourceMap": false, 69 | "namedChunks": false, 70 | "extractLicenses": true, 71 | "fileReplacements": [ 72 | { 73 | "replace": "src/environments/environment.ts", 74 | "with": "src/environments/environment.prod.ts" 75 | } 76 | ] 77 | } 78 | } 79 | }, 80 | "serve": { 81 | "builder": "@angular-devkit/build-angular:dev-server", 82 | "options": { 83 | "buildTarget": "pinboard-pin:build" 84 | }, 85 | "configurations": { 86 | "production": { 87 | "buildTarget": "pinboard-pin:build:production" 88 | } 89 | } 90 | }, 91 | "extract-i18n": { 92 | "builder": "@angular-devkit/build-angular:extract-i18n", 93 | "options": { 94 | "buildTarget": "pinboard-pin:build" 95 | } 96 | }, 97 | "lint": { 98 | "builder": "@angular-devkit/build-angular:tslint", 99 | "options": { 100 | "tsConfig": [ 101 | "tsconfig.app.json" 102 | ], 103 | "exclude": [] 104 | } 105 | } 106 | } 107 | } 108 | }, 109 | "schematics": { 110 | "@schematics/angular:component": { 111 | "type": "component" 112 | }, 113 | "@schematics/angular:directive": { 114 | "type": "directive" 115 | }, 116 | "@schematics/angular:service": { 117 | "type": "service" 118 | }, 119 | "@schematics/angular:guard": { 120 | "typeSeparator": "." 121 | }, 122 | "@schematics/angular:interceptor": { 123 | "typeSeparator": "." 124 | }, 125 | "@schematics/angular:module": { 126 | "typeSeparator": "." 127 | }, 128 | "@schematics/angular:pipe": { 129 | "typeSeparator": "." 130 | }, 131 | "@schematics/angular:resolver": { 132 | "typeSeparator": "." 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/app/options/options.component.ts: -------------------------------------------------------------------------------- 1 | // this component is the user setting dialog displayed under options 2 | 3 | import { Component, OnInit, OnDestroy, ChangeDetectorRef } from "@angular/core"; 4 | import { CommonModule } from "@angular/common"; 5 | import { FormsModule, NgForm } from "@angular/forms"; 6 | import { Options, StorageService } from "../storage.service"; 7 | 8 | interface MessagePayload { 9 | options?: Options; 10 | } 11 | 12 | // Options form 13 | 14 | @Component({ 15 | selector: "app-options", 16 | templateUrl: "./options.component.html", 17 | styleUrls: ["./options.component.scss"], 18 | imports: [CommonModule, FormsModule], 19 | }) 20 | export class OptionsComponent implements OnInit, OnDestroy { 21 | options: Options; 22 | 23 | shortcut: string; // default keyboard shortcut 24 | 25 | page: string; // type of page (popup or options) 26 | 27 | theme = "light"; // color scheme of the page 28 | 29 | private readonly messageListener: (message: MessagePayload) => void; 30 | 31 | constructor(private storage: StorageService, private cdr: ChangeDetectorRef) { 32 | this.messageListener = this.onMessage.bind(this) as ( 33 | message: MessagePayload 34 | ) => void; 35 | } 36 | 37 | ngOnInit() { 38 | this.page = (this.storage.getInfo("options.page") as string) || "options"; 39 | this.storage.getOptions().subscribe((options) => { 40 | this.options = options; 41 | this.setTheme(); 42 | this.setOnMessageListener(true); 43 | this.cdr.detectChanges(); 44 | }); 45 | void browser.commands.getAll().then((commands) => { 46 | for (const command of commands) { 47 | if (command.name === "_execute_browser_action") { 48 | this.shortcut = command.shortcut; 49 | } 50 | } 51 | this.cdr.detectChanges(); 52 | }); 53 | } 54 | 55 | ngOnDestroy() { 56 | this.setOnMessageListener(false); 57 | } 58 | 59 | setTheme() { 60 | this.theme = 61 | this.options.dark === true || 62 | (this.options.dark !== false && 63 | window.matchMedia("(prefers-color-scheme: dark)").matches) 64 | ? "dark" 65 | : "light"; 66 | } 67 | 68 | // check whether given options are the same 69 | sameOptions(options: Options): boolean { 70 | const opts: Partial = { ...options }; 71 | for (const key in this.options) { 72 | if (typeof this.options[key] === "boolean" && key !== "dark") { 73 | opts[key as keyof Options] = !!opts[key as keyof Options]; 74 | } 75 | } 76 | for (const key in opts) { 77 | if (opts[key as keyof Options] !== this.options[key as keyof Options]) { 78 | return false; 79 | } 80 | } 81 | return true; 82 | } 83 | 84 | // set the listener for internal messages 85 | setOnMessageListener(on: boolean) { 86 | const event = browser.runtime.onMessage; 87 | const listener = this.messageListener; 88 | if (event.hasListener(listener)) { 89 | if (!on) { 90 | void event.removeListener(listener); 91 | } 92 | } else { 93 | if (on) { 94 | void event.addListener(listener); 95 | } 96 | } 97 | } 98 | 99 | // fires when another process connects 100 | // this synchronizes the settings if options popup and options page 101 | // are open at the same time 102 | onMessage(message: MessagePayload): void { 103 | const options = message.options; 104 | if (options && !this.sameOptions(options)) { 105 | this.options = options; 106 | this.setTheme(); 107 | this.cdr.detectChanges(); // run change detection 108 | } 109 | } 110 | 111 | // submit form (store options in local storage) 112 | submit(form: NgForm) { 113 | if (!form.valid) { 114 | return false; 115 | } 116 | const value: Options = form.value as Options; 117 | if (!value || this.sameOptions(value)) { 118 | return false; 119 | } 120 | this.options = value; 121 | this.setTheme(); 122 | this.storage.setOptions(value).subscribe(); 123 | void browser.runtime.sendMessage({ options: value }); 124 | return false; 125 | } 126 | 127 | // close the options popup 128 | close() { 129 | if (this.page === "popup") { 130 | window.close(); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/app/pinpage/pinpage.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | @if (!ready) { 4 |

Loading page from Pinboard...

5 | } 6 | @if (ready && !update) { 7 |

Save page to Pinboard

8 | } 9 | @if (ready && update) { 10 |

Update page in Pinboard

11 | } 12 | 13 |
14 | 17 | 20 | 23 | 26 | 29 | 32 | 35 | 38 | 41 | 44 | 47 |
48 | 49 | @if (!error) { 50 |
51 |
52 | 53 | @if (update && !date) { 54 | 55 | This page has already been added as a bookmark to Pinboard. 56 | 57 | }@if (update && date) { 58 | 59 | This page has already been added as a bookmark to Pinboard {{date | ago}}. 60 | 61 | }@if (!update) { 62 | 63 | This page has not yet been added as a bookmark to Pinboard. 64 | 65 | } 66 |
67 |
68 | 69 | 70 |
71 |
72 | 73 | 75 |
76 |
77 | 78 | 81 |
82 |
83 | @if (ready && tagsFocus && completions) { 84 |
    85 | @for (tag of completions; track tag; let i = $index) { 86 |
  • {{tag}}
  • 88 | } 89 |
90 | } 91 |
92 |
93 | 94 | 97 |
98 | @if (suggested && suggested.length) { 99 |
100 | 101 | 102 | @for (tag of suggested; track tag) { 103 | {{tag}} 104 | } 105 | Add all 106 | 107 |
108 | } 109 | @if (popular && popular.length) { 110 |
111 | 112 | 113 | @for (tag of popular; track tag) { 114 | {{tag}} 115 | } 116 | Add all 117 | 118 |
119 | } 120 | @if (keywords && keywords.length) { 121 |
122 | 123 | 124 | @for (tag of keywords; track tag) { 125 | {{tag}} 126 | } 127 | Add all 128 | 129 |
130 | } 131 |
132 | 133 | 134 | 135 | 136 | 137 | 138 | @if (update) { 139 | 141 | } 142 | 145 |
146 |
147 | } 148 | 149 | @if (error) { 150 |

{{error}}

151 | } 152 | 153 | @if (error) { 154 |
155 |
156 | 157 | 158 | @if (retry) { 159 | 160 | } 161 |
162 |
163 | } 164 | 165 |
-------------------------------------------------------------------------------- /src/app/background/background.component.ts: -------------------------------------------------------------------------------- 1 | // this component pings the saved state of pages in the background 2 | 3 | import { Component, OnInit, OnDestroy } from "@angular/core"; 4 | import { StorageService } from "../storage.service"; 5 | import { PinboardService } from "../pinboard.service"; 6 | import { Post } from "../pinpage/pinpage.component"; 7 | import { IconService } from "../icon.service"; 8 | import { Options } from "../storage.service"; 9 | import { errorMessage, logError } from "../util"; 10 | 11 | // Background page used for checking whether pages are saved in Pinboard 12 | 13 | type UpdatedChangeInfo = { 14 | url?: string; 15 | }; 16 | 17 | type UpdatedHandler = ( 18 | tabId: number, 19 | changeInfo: UpdatedChangeInfo, 20 | tab: browser.tabs.Tab 21 | ) => void; 22 | 23 | interface MessagePayload { 24 | options?: Options; 25 | } 26 | 27 | type MessageHandler = (message: MessagePayload) => void; 28 | 29 | type MenuHandler = ( 30 | info: browser.menus.OnClickData, 31 | tab: browser.tabs.Tab 32 | ) => void; 33 | 34 | @Component({ 35 | selector: "app-background", 36 | template: "

Background page for Pinboard

", 37 | }) 38 | export class BackgroundComponent implements OnInit, OnDestroy { 39 | private readonly updatedListener: UpdatedHandler; 40 | private readonly messageListener: MessageHandler; 41 | private readonly menuListener: MenuHandler; 42 | 43 | constructor( 44 | private storage: StorageService, 45 | private pinboard: PinboardService, 46 | private icon: IconService 47 | ) { 48 | this.updatedListener = this.onUpdated.bind(this) as UpdatedHandler; 49 | this.messageListener = this.onMessage.bind(this) as MessageHandler; 50 | this.menuListener = this.onMenuClicked.bind(this) as MenuHandler; 51 | } 52 | 53 | private ping = false; 54 | private menu = false; 55 | private toread = false; 56 | private unshared = false; 57 | 58 | ngOnInit() { 59 | this.storage.getOptions().subscribe((options) => { 60 | this.setOnMessageListener(true); 61 | this.onMessage({ options }); 62 | }); 63 | } 64 | 65 | ngOnDestroy() { 66 | this.setOnUpdateListener(false); 67 | this.setOnMessageListener(false); 68 | this.setOnMenuClickedListener(false); 69 | } 70 | 71 | // set the listener for internal messages 72 | setOnMessageListener(on: boolean) { 73 | const event = browser.runtime.onMessage; 74 | const listener = this.messageListener; 75 | if (event.hasListener(listener)) { 76 | if (!on) { 77 | event.removeListener(listener); 78 | } 79 | } else { 80 | if (on) { 81 | event.addListener(listener); 82 | } 83 | } 84 | } 85 | 86 | // fires when another process connects 87 | onMessage(message: MessagePayload): void { 88 | const options: Options | undefined = message.options; 89 | if (options) { 90 | if (options.ping !== this.ping) { 91 | this.setOnUpdateListener(options.ping); 92 | } 93 | if (options.menu !== this.menu) { 94 | this.setOnMenuClickedListener(options.menu); 95 | } 96 | this.toread = options.toread; 97 | this.unshared = options.unshared; 98 | } 99 | } 100 | 101 | // set the listener for url update in browser tabs 102 | setOnUpdateListener(on: boolean) { 103 | const event = browser.tabs.onUpdated; 104 | const listener = this.updatedListener; 105 | if (event.hasListener(listener)) { 106 | if (!on) { 107 | void event.removeListener(listener); 108 | } 109 | } else { 110 | if (on) { 111 | void event.addListener(listener); 112 | } 113 | } 114 | this.ping = on; 115 | } 116 | 117 | // fires when the active tab in a window changes 118 | onUpdated( 119 | tabId: number, 120 | changeInfo: UpdatedChangeInfo, 121 | tab: browser.tabs.Tab 122 | ): void { 123 | // do not ping Pinboard in incognito mode 124 | if (tab.incognito) { 125 | return; 126 | } 127 | const url = changeInfo.url; 128 | if (url) { 129 | if (/^https?:\/\//.test(url)) { 130 | this.pinboard.get(url).subscribe({ 131 | next: (data: { posts?: unknown[] }) => 132 | this.icon.setIcon( 133 | tabId, 134 | !!(data && data.posts && data.posts.length) 135 | ), 136 | error: () => this.icon.setIcon(tabId, false), 137 | }); 138 | } else { 139 | this.icon.setIcon(tabId, false); 140 | } 141 | } 142 | } 143 | 144 | // set the listener for context menu clicks 145 | setOnMenuClickedListener(on: boolean) { 146 | const menus = browser.contextMenus; 147 | const event = menus.onClicked; 148 | const listener = this.menuListener; 149 | if (event.hasListener(listener)) { 150 | if (!on) { 151 | void event.removeListener(listener); 152 | void menus.remove("save-link-to-pinboard"); 153 | void menus.remove("save-page-to-pinboard"); 154 | } 155 | } else { 156 | if (on) { 157 | browser.contextMenus.create({ 158 | id: "save-page-to-pinboard", 159 | title: "Save page to Pinboard", 160 | contexts: ["page"], 161 | documentUrlPatterns: ["http://*/*", "https://*/*"], 162 | command: "_execute_browser_action", 163 | }); 164 | void browser.contextMenus.create({ 165 | id: "save-link-to-pinboard", 166 | title: "Save link to Pinboard", 167 | contexts: ["link"], 168 | targetUrlPatterns: ["http://*/*", "https://*/*"], 169 | }); 170 | void event.addListener(listener); 171 | } 172 | } 173 | this.menu = on; 174 | } 175 | 176 | // handle menu clicks for saving links in the background 177 | onMenuClicked(info: browser.menus.OnClickData, _tab: browser.tabs.Tab): void { 178 | if (info.menuItemId === "save-link-to-pinboard" && info.linkUrl) { 179 | const url = info.linkUrl; 180 | // since we cannot get the actual title, we use the link text instead 181 | const title = info.linkText || "Link saved with Pinboard Pin"; 182 | const post: Post = { 183 | url, 184 | title, 185 | description: "", 186 | tags: "", 187 | unshared: this.unshared, 188 | toread: this.toread, 189 | // links will be only added, 190 | // we do not want to overwrite existing titles and descriptions 191 | noreplace: true, 192 | }; 193 | this.pinboard.save(post).subscribe({ 194 | next: (error: string | null) => { 195 | if (error && !/already exists/.test(error)) { 196 | this.saveLinkError(error); 197 | } 198 | }, 199 | error: (error: unknown) => { 200 | this.saveLinkError(errorMessage(error)); 201 | }, 202 | }); 203 | } 204 | } 205 | 206 | saveLinkError(error: string): void { 207 | logError(error); 208 | // we cannot display an alert directly, so we use a workaround 209 | const showAlert = 'alert("Could not save the link to Pinboard.")'; 210 | void browser.tabs.executeScript(null, { code: showAlert }); 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /src/app/pinboard.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from "@angular/core"; 2 | 3 | import { throwError, Observable, of, forkJoin, from } from "rxjs"; 4 | import { catchError, filter, map, mergeMap, switchMap } from "rxjs/operators"; 5 | 6 | import { StorageService } from "./storage.service"; 7 | import { Post } from "./pinpage/pinpage.component"; 8 | import { 9 | HttpClient, 10 | HttpParameterCodec, 11 | HttpParams, 12 | } from "@angular/common/http"; 13 | 14 | export const pinboardPage = "https://pinboard.in/"; 15 | 16 | export const passwordPage = pinboardPage + "settings/password"; 17 | const tabsPage = pinboardPage + "tabs/"; 18 | 19 | const apiUrl = "https://api.pinboard.in/v1/"; 20 | 21 | const cacheTimeout = 1000 * 60 * 60; // one hour cache time 22 | 23 | // Create a custom encoder for query parameters that can be used 24 | // as a workaround for https://github.com/angular/angular/issues/18261 25 | // in order to fix https://github.com/Cito/Pinboard-Pin/issues/17 26 | 27 | class ParamsEncoder implements HttpParameterCodec { 28 | encodeKey(key: string): string { 29 | return encodeURIComponent(key); 30 | } 31 | 32 | encodeValue(value: string): string { 33 | return encodeURIComponent(value); 34 | } 35 | 36 | decodeKey(key: string): string { 37 | return decodeURIComponent(key); 38 | } 39 | 40 | decodeValue(value: string): string { 41 | return decodeURIComponent(value); 42 | } 43 | } 44 | 45 | const paramsEncoder = new ParamsEncoder(); 46 | 47 | // Service for dealing with the Pinboard API 48 | 49 | @Injectable({ providedIn: "root" }) 50 | export class PinboardService { 51 | constructor(private http: HttpClient, private storage: StorageService) {} 52 | 53 | // get an object via the Pinboard API 54 | httpGet( 55 | method: string, 56 | params?: Record 57 | ): Observable { 58 | const p: Record = params ?? {}; 59 | if (!p.auth_token) { 60 | return this.storage.get("token").pipe( 61 | switchMap((token) => { 62 | if (typeof token !== "string" || !token) { 63 | return throwError(() => new Error("No API token!")); 64 | } 65 | p.auth_token = token; 66 | return this.httpGet(method, p); 67 | }) 68 | ); 69 | } 70 | p.format = "json"; 71 | const httpParams = Object.entries(p).reduce( 72 | (params: HttpParams, [key, value]: [string, string]) => 73 | params.set(key, value), 74 | new HttpParams({ encoder: paramsEncoder }) 75 | ); 76 | return this.http.get(apiUrl + method, { params: httpParams }); 77 | } 78 | 79 | // check the given API token and memorize it if valid 80 | setToken(value: string): Observable { 81 | const values = value.split(":", 2); 82 | if (values.length !== 2) { 83 | return of(false); 84 | } 85 | return this.httpGet<{ result: string }>("user/api_token", { 86 | auth_token: value, 87 | }).pipe( 88 | map((data) => data.result === values[1]), 89 | switchMap((ok) => 90 | ok 91 | ? this.storage.set({ token: value }).pipe(map(() => true)) 92 | : of(false) 93 | ) 94 | ); 95 | } 96 | 97 | // check whether we still need an API token 98 | get needToken(): Observable { 99 | return this.storage.get("token").pipe(map((res) => !res)); 100 | } 101 | 102 | // extract user name from API token 103 | get userName(): Observable { 104 | return this.storage.get("token").pipe( 105 | map((res) => 106 | typeof res === "string" ? res.split(":", 1)[0] : undefined 107 | ), 108 | filter((res): res is string => !!res) 109 | ); 110 | } 111 | 112 | // forget the API token 113 | forgetToken(): Observable { 114 | // also clear the tags cache when leaving 115 | return this.storage.remove(["token", "tags"]); 116 | } 117 | 118 | // get bookmark with the given url 119 | get(url: string): Observable { 120 | return this.httpGet("posts/get", { url: url, meta: "no" }); 121 | } 122 | 123 | // add or replace bookmark with given attributes 124 | save(post: Post): Observable { 125 | const params: Record = { 126 | url: post.url, 127 | description: post.title, 128 | dt: new Date().toISOString(), 129 | }; 130 | if (post.description) { 131 | params.extended = post.description; 132 | } 133 | if (post.tags) { 134 | params.tags = post.tags; 135 | } 136 | params.replace = post.noreplace ? "no" : "yes"; 137 | params.shared = post.unshared ? "no" : "yes"; 138 | params.toread = post.toread ? "yes" : "no"; 139 | return this.httpGet<{ result_code: string }>("posts/add", params).pipe( 140 | map((res) => (res.result_code === "done" ? null : res.result_code)) 141 | ); 142 | } 143 | 144 | // delete bookmark with the given url 145 | delete(url: string): Observable { 146 | return this.httpGet("posts/delete", { url: url }); 147 | } 148 | 149 | // get suggested tags for the given url 150 | suggest( 151 | url: string 152 | ): Observable<{ popular: string[]; recommended: string[] }> { 153 | return this.httpGet>( 154 | "posts/suggest", 155 | { url: url } 156 | ).pipe( 157 | map((data) => { 158 | const tags = { popular: [] as string[], recommended: [] as string[] }; 159 | for (const d of data) { 160 | if (d.popular) { 161 | tags.popular.push(...d.popular); 162 | } 163 | if (d.recommended) { 164 | tags.recommended.push(...d.recommended); 165 | } 166 | } 167 | return tags; 168 | }) 169 | ); 170 | } 171 | 172 | // get post for the given url together with the suggested tags 173 | getAndSuggest(url: string): Observable { 174 | return forkJoin([this.get(url), this.suggest(url)]).pipe( 175 | map(([post, tags]) => 176 | Object.assign(post as Record, tags) 177 | ) 178 | ); 179 | } 180 | 181 | // get the list of all used tags (with numeric tag counters) 182 | tags(): Observable<{ [tag: string]: number }> { 183 | return this.httpGet>("tags/get").pipe( 184 | map((tags) => { 185 | const result: Record = {}; 186 | for (const tag of Object.keys(tags)) { 187 | result[tag] = +tags[tag]; 188 | } 189 | return result; 190 | }) 191 | ); 192 | } 193 | 194 | // get a cached list of all used tags 195 | cachedTags(): Observable<{ [tag: string]: number }> { 196 | return this.storage.get("tags").pipe( 197 | switchMap( 198 | ( 199 | cached: { tags?: { [tag: string]: number }; date?: number } | null 200 | ) => { 201 | const date = Date.now(); 202 | const needsRefresh = 203 | !cached || 204 | !cached.tags || 205 | !cached.date || 206 | cached.tags instanceof Array || // old version used arrays 207 | cached.date > date || 208 | date - cached.date > cacheTimeout; 209 | 210 | if (needsRefresh) { 211 | return this.tags().pipe( 212 | switchMap((freshTags) => 213 | this.storage.set({ tags: { tags: freshTags, date } }).pipe( 214 | catchError(() => of(null)), // ignore cache write failures 215 | map(() => freshTags) 216 | ) 217 | ), 218 | catchError(() => of({} as { [tag: string]: number })) 219 | ); 220 | } 221 | 222 | return of(cached.tags as { [tag: string]: number }); 223 | } 224 | ) 225 | ); 226 | } 227 | 228 | // update the cached object with all used tags and their frequency 229 | updateTagCache(addTags: string[], savedTags: string[]): Observable { 230 | return this.storage.get("tags").pipe( 231 | mergeMap( 232 | (cache: { tags?: { [tag: string]: number }; date?: number } | null) => { 233 | let tags: { [tag: string]: number }, date: number; 234 | if (cache && cache.tags && cache.date) { 235 | tags = cache.tags; 236 | date = cache.date; 237 | } else { 238 | tags = {}; 239 | date = Date.now(); 240 | } 241 | for (const tag of savedTags) { 242 | if (!addTags.includes(tag)) { 243 | if (Object.hasOwn(tags, tag)) { 244 | if (--tags[tag] <= 0) { 245 | delete tags[tag]; 246 | } 247 | } 248 | } 249 | } 250 | for (const tag of addTags) { 251 | if (!savedTags.includes(tag)) { 252 | if (Object.hasOwn(tags, tag)) { 253 | ++tags[tag]; 254 | } else { 255 | tags[tag] = 1; 256 | } 257 | } 258 | } 259 | return this.storage.set({ tags: { tags: tags, date: date } }); 260 | } 261 | ) 262 | ); 263 | } 264 | 265 | // save the current tabs as tabset using the web form 266 | // (this operation is not provided by the Pinboard API) 267 | saveTabs(data: any): Observable { 268 | const params = new FormData(); 269 | params.append("data", JSON.stringify(data)); 270 | const post = this.http.post(tabsPage + "save/", params); 271 | const show = from(browser.tabs.create({ url: tabsPage + "show/" })); 272 | return post.pipe(switchMap(() => show)); 273 | } 274 | 275 | // check if the given URL is valid (can be saved in Pinboard) 276 | isValidUrl(url: string): boolean { 277 | return url && /:\/\//.test(url); 278 | } 279 | } 280 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /src/app/pinpage/pinpage.component.ts: -------------------------------------------------------------------------------- 1 | // this component is the save bookmark dialog displayed in the popup 2 | 3 | import { 4 | Component, 5 | ElementRef, 6 | OnInit, 7 | OnDestroy, 8 | ChangeDetectorRef, 9 | } from "@angular/core"; 10 | import { FormsModule, NgForm } from "@angular/forms"; 11 | import { Router } from "@angular/router"; 12 | import { CommonModule } from "@angular/common"; 13 | import { AgoPipe } from "../interval.pipe"; 14 | 15 | import { Subscription, Subject, timer } from "rxjs"; 16 | import { debounceTime, distinctUntilChanged, finalize } from "rxjs/operators"; 17 | 18 | import { IconService } from "../icon.service"; 19 | import { PinboardService, pinboardPage } from "../pinboard.service"; 20 | import { Options, StorageService } from "../storage.service"; 21 | import { errorMessage, logError } from "../util"; 22 | 23 | const debounceDueTime = 250; // timeout in ms for reacting to changes 24 | const maxCompletions = 9; // maximum number of suggested completions 25 | 26 | export interface Post { 27 | url: string; 28 | title: string; 29 | description: string; 30 | tags: string; 31 | unshared: boolean; 32 | toread: boolean; 33 | noreplace: boolean; 34 | } 35 | 36 | interface Content { 37 | url: string; 38 | title: string; 39 | description: string; 40 | keywords: string[]; 41 | } 42 | 43 | interface RawContent { 44 | url: string; 45 | title: string; 46 | selection: string; 47 | description: string; 48 | keywords: string; 49 | } 50 | 51 | // Pin page form 52 | // TODO: This class is too big, should be refactored. 53 | // At least the tag handling should go into sub component(s). 54 | 55 | @Component({ 56 | selector: "app-popup", 57 | templateUrl: "./pinpage.component.html", 58 | styleUrls: ["./pinpage.component.scss"], 59 | imports: [CommonModule, FormsModule, AgoPipe], 60 | }) 61 | export class PinPageComponent implements OnInit, OnDestroy { 62 | url: string; 63 | title: string; 64 | description: string; // description 65 | tags: string; // current tags 66 | savedTags: string; // tags already saved for this URL 67 | allTags: { [tag: string]: number }; // all of our tags with frequency 68 | suggested: string[]; // recommended tags from our own 69 | popular: string[]; // other popular tags 70 | keywords: string[]; // keywords taken from the page 71 | completions: string[]; // tag completions 72 | tagsFocus: boolean; // whether the tags field has focus 73 | tagSelected: number; // index of the selected tag 74 | unshared: boolean; 75 | toread: boolean; 76 | ready: boolean; 77 | update: boolean; 78 | date: string; 79 | error: string | null; 80 | retry: boolean; 81 | options: Options; 82 | 83 | theme = "light"; // color scheme of the page 84 | 85 | private tagsSubject = new Subject(); 86 | private tagsSubscription: Subscription; 87 | 88 | constructor( 89 | private pinboard: PinboardService, 90 | private storage: StorageService, 91 | private icon: IconService, 92 | private router: Router, 93 | private eref: ElementRef, 94 | private cdr: ChangeDetectorRef 95 | ) {} 96 | 97 | ngOnInit() { 98 | this.ready = false; 99 | this.update = false; 100 | this.error = null; 101 | this.retry = false; 102 | this.storage.getOptions().subscribe((options) => { 103 | this.options = options; 104 | this.setTheme(); 105 | const getContent = 106 | options.meta || options.selection 107 | ? browser.tabs 108 | .executeScript(null, { file: "/js/content.js" }) 109 | .then((content: Array) => 110 | this.processContent(content[0]) 111 | ) 112 | .catch(() => this.getContent()) 113 | : this.getContent(); 114 | void getContent.then( 115 | (content: Content) => { 116 | this.setContent(content); 117 | this.cdr.detectChanges(); 118 | }, 119 | (error: unknown) => { 120 | this.logError("Can only pin normal web pages.", errorMessage(error)); 121 | this.cdr.detectChanges(); 122 | } 123 | ); 124 | }); 125 | this.tagsFocus = false; 126 | this.tagsSubscription = this.tagsSubject 127 | .pipe(debounceTime(debounceDueTime), distinctUntilChanged()) 128 | .subscribe((value: string) => this.tagsChanged(value)); 129 | } 130 | 131 | ngOnDestroy() { 132 | this.tagsSubscription.unsubscribe(); 133 | } 134 | 135 | setTheme() { 136 | this.theme = 137 | this.options.dark === true || 138 | (this.options.dark !== false && 139 | window.matchMedia("(prefers-color-scheme: dark)").matches) 140 | ? "dark" 141 | : "light"; 142 | } 143 | 144 | // process the data gathered by the content script 145 | processContent(content: RawContent): Content { 146 | let { url, title } = content; 147 | url = url || null; 148 | title = title 149 | ? title.length > 255 // trim title 150 | ? title.slice(0, 254) + "\u2026" 151 | : title 152 | : null; 153 | const options = this.options; 154 | let description = options.selection ? content.selection : null; 155 | if (!description && options.meta) { 156 | description = content.description; 157 | } 158 | description = description 159 | ? description.length > 3200 160 | ? // trim description (actual max. size seems to be 3798 chars) 161 | description.slice(0, 3199) + "\u2026" 162 | : description 163 | : null; 164 | let keywords: string[] = []; 165 | if (options.meta && content.keywords) { 166 | for (let word of content.keywords.split(",")) { 167 | word = word.replace(/\s+/, "").slice(0, 255).toLowerCase(); 168 | if (word && !keywords.includes(word)) { 169 | keywords.push(word); 170 | if (keywords.length >= 100) { 171 | break; 172 | } 173 | } 174 | } 175 | } 176 | keywords = keywords.length ? keywords.slice(0, 6400) : null; 177 | return { url, title, description, keywords }; 178 | } 179 | 180 | // get url and title of content (used if content script cannot run) 181 | getContent(): Promise { 182 | return browser.tabs 183 | .query({ active: true, currentWindow: true }) 184 | .then((tabs) => tabs[0]) 185 | .then((tab) => ({ 186 | url: tab.url, 187 | title: tab.title, 188 | description: null, 189 | keywords: null, 190 | })); 191 | } 192 | 193 | // store info on current content in the form inputs 194 | setContent(content: Content): void { 195 | if (content && content.url && this.pinboard.isValidUrl(content.url)) { 196 | this.url = content.url; 197 | this.title = content.title; 198 | this.description = content.description; 199 | if (this.description && this.options.blockquote) { 200 | this.description = 201 | "
" + 202 | this.description.slice(0, 3200 - 25) + 203 | "
"; 204 | } 205 | this.keywords = content.keywords; 206 | this.tags = null; 207 | this.unshared = this.options.unshared; 208 | this.toread = this.options.toread; 209 | this.retry = true; 210 | this.suggested = this.popular = null; 211 | // query both page data and suggested tags 212 | this.pinboard.getAndSuggest(this.url).subscribe({ 213 | next: (data: unknown) => 214 | this.setData( 215 | data as { 216 | posts?: Array<{ 217 | href: string; 218 | description: string; 219 | extended: string; 220 | tags: string; 221 | shared: string; 222 | toread: string; 223 | }>; 224 | } & { popular?: string[]; recommended?: string[] } & { 225 | date?: string; 226 | } 227 | ), 228 | error: (error: unknown) => 229 | this.logError( 230 | "Cannot check this page on Pinboard.", 231 | errorMessage(error) 232 | ), 233 | }); 234 | } else { 235 | this.logError( 236 | "Can only pin normal web pages.", 237 | "Cannot get the URL of the page" 238 | ); 239 | } 240 | } 241 | 242 | // receive page data and suggested tags from parallel queries 243 | setData( 244 | data: { 245 | posts?: Array<{ 246 | href: string; 247 | description: string; 248 | extended: string; 249 | tags: string; 250 | shared: string; 251 | toread: string; 252 | }>; 253 | } & { popular?: string[]; recommended?: string[] } & { date?: string } 254 | ): void { 255 | if (data.posts && data.posts.length) { 256 | this.date = data?.date; 257 | const post = data.posts[0]; 258 | this.url = post.href; 259 | this.title = post.description; 260 | this.description = post.extended; 261 | this.tags = post.tags; 262 | this.unshared = post.shared !== "yes"; 263 | this.toread = post.toread === "yes"; 264 | this.update = true; 265 | // set browser icon to saved state 266 | void browser.tabs.query({ url: this.url }).then( 267 | (tabs: browser.tabs.Tab[]) => { 268 | for (const tab of tabs) { 269 | this.icon.setIcon(tab.id, true); 270 | } 271 | }, 272 | (error: unknown) => logError(error) 273 | ); 274 | } 275 | // Note: "popular" and "recommended" are interchanged in Pinboard 276 | if (data.popular) { 277 | this.suggested = data.popular; 278 | } 279 | if (this.options.popular && data.recommended) { 280 | this.popular = data.recommended; 281 | } 282 | this.pinboard 283 | .cachedTags() 284 | .pipe(finalize(() => this.cdr.detectChanges())) 285 | .subscribe({ 286 | next: (tags) => { 287 | this.allTags = tags; 288 | if (this.tags) { 289 | this.tags = this.tags.trim(); 290 | this.savedTags = this.tags; 291 | this.tags += " "; 292 | } else { 293 | this.savedTags = null; 294 | } 295 | this.completions = null; 296 | this.setReady(); 297 | }, 298 | }); 299 | } 300 | 301 | // set form as ready for input 302 | setReady(): void { 303 | this.ready = true; 304 | this.cdr.detectChanges(); 305 | // wait until inputs have been enabled, then focus 306 | timer(0).subscribe(() => { 307 | const focus = this.url 308 | ? this.title 309 | ? this.tags && !this.description 310 | ? "description" 311 | : "tags" 312 | : "title" 313 | : "url"; 314 | const element = (this.eref.nativeElement as HTMLElement).querySelector( 315 | "#" + focus 316 | ); 317 | if (element instanceof HTMLElement) { 318 | element.focus(); 319 | } 320 | }); 321 | } 322 | 323 | // check whether the given tags have already been added 324 | hasTags(tags: string | string[]): boolean { 325 | if (!this.tags) { 326 | return false; 327 | } 328 | if (!Array.isArray(tags)) { 329 | tags = [tags]; 330 | } 331 | const allTags = this.tags.split(" ").filter((tag) => !!tag); 332 | return tags.every((tag) => allTags.includes(tag)); 333 | } 334 | 335 | // add the given tags if they have not already been added, otherwise remove 336 | addTags(tags: string | string[]): void { 337 | if (!Array.isArray(tags)) { 338 | tags = [tags]; 339 | } 340 | let allTags = (this.tags || "").split(" ").filter((tag) => !!tag); 341 | const newTags = tags.filter((tag) => !allTags.includes(tag)); 342 | if (newTags.length) { 343 | // some tags are new 344 | allTags.push(...newTags); // add these tags 345 | } else { 346 | // all tags have already been added, remove these tags again 347 | allTags = allTags.filter((tag) => !tags.includes(tag)); 348 | } 349 | this.tags = allTags.join(" "); 350 | } 351 | 352 | // this method is called when keys have been pressed down in the tabs field 353 | tagsKeyDown(event: KeyboardEvent): boolean { 354 | if (!this.ready || !this.tagsFocus || !this.completions) { 355 | return true; 356 | } 357 | // Firefox reacts to some of our control keys as well, so to prevent this 358 | // from happening, we have to listen here before the key has been pressed 359 | let control = true; 360 | switch (event.code) { 361 | case "Home": 362 | this.tagSelected = 0; 363 | break; 364 | case "End": 365 | this.tagSelected = this.completions.length - 1; 366 | break; 367 | case "ArrowDown": 368 | if (this.tagSelected < this.completions.length - 1) { 369 | ++this.tagSelected; 370 | } 371 | break; 372 | case "ArrowUp": 373 | if (this.tagSelected > 0) { 374 | --this.tagSelected; 375 | } 376 | break; 377 | case "Enter": 378 | case "Tab": 379 | case "ArrowRight": 380 | const tag = this.completions[this.tagSelected]; 381 | const inputElement = event.target as HTMLInputElement; 382 | let value = inputElement.value; 383 | const words = value.split(" "); 384 | if (words.length) { 385 | words.pop(); 386 | } 387 | if (!words.includes(tag)) { 388 | words.push(tag); 389 | value = words.join(" ") + " "; 390 | this.tagsChanged(value); 391 | this.tags = value; 392 | } 393 | break; 394 | default: 395 | control = false; 396 | } 397 | return !control; 398 | } 399 | 400 | // this method is called when keys have been released in the tabs field 401 | tagsKeyUp(event: KeyboardEvent): boolean { 402 | // the field value is changed after the key has been pressed, 403 | // so this is the right moment for checking for value changes 404 | if (this.ready && this.tagsFocus) { 405 | this.tagsSubject.next((event.target as HTMLInputElement).value); 406 | } 407 | return true; 408 | } 409 | 410 | // this method is called with debounce when tags have changed 411 | // it must then determine the list of tag completions 412 | tagsChanged(tags: string): void { 413 | const words = tags.replace(",", " ").split(" "); 414 | let word = words.length ? words.pop() : null; 415 | const allTags = this.allTags; 416 | const matches: [string, number][] = []; 417 | const alpha = this.options.alpha; 418 | if (word) { 419 | word = word.toLowerCase(); 420 | for (const tag of Object.keys(allTags)) { 421 | if (tag.toLowerCase().startsWith(word) && !words.includes(tag)) { 422 | matches.push([tag, alpha ? 0 : allTags[tag]]); 423 | } 424 | } 425 | } 426 | // sort matching tags by decreasing frequency 427 | matches.sort( 428 | (a: [string, number], b: [string, number]) => 429 | b[1] - a[1] || a[0].localeCompare(b[0]) 430 | ); 431 | matches.splice(maxCompletions); 432 | const completions: string[] = matches.map((a) => a[0]).reverse(); 433 | if (completions.length) { 434 | const oldCompletions = this.completions; 435 | if ( 436 | !oldCompletions || 437 | completions.length !== oldCompletions.length || 438 | completions.some((tag, i) => completions[i] !== oldCompletions[i]) 439 | ) { 440 | this.completions = completions; 441 | this.tagSelected = completions.length - 1; 442 | } 443 | } else { 444 | this.completions = null; 445 | } 446 | } 447 | 448 | // this method is called when a tag completion was clicked 449 | selectCompletion(tag: string): boolean { 450 | let value = this.tags || ""; 451 | const words = value.split(" "); 452 | if (words.length) { 453 | words.pop(); 454 | } 455 | if (!words.includes(tag)) { 456 | words.push(tag); 457 | value = words.join(" ") + " "; 458 | this.tagsChanged(value); 459 | this.tags = value; 460 | } 461 | return false; 462 | } 463 | 464 | // delete the current bookmark 465 | remove(): boolean { 466 | if (this.ready && this.update && this.url) { 467 | this.pinboard.delete(this.url).subscribe({ 468 | next: () => { 469 | // update the tags in the cache 470 | const savedTags = this.savedTags ? this.savedTags.split(" ") : []; 471 | this.pinboard 472 | .updateTagCache([], savedTags) 473 | .pipe( 474 | finalize( 475 | // set the browser icon to unsaved state 476 | () => 477 | void browser.tabs.query({ url: this.url }).then( 478 | (tabs: browser.tabs.Tab[]) => { 479 | for (const tab of tabs) { 480 | this.icon.setIcon(tab.id, false); 481 | } 482 | this.cancel(); 483 | }, 484 | (error: unknown) => { 485 | logError(error); 486 | this.cancel(); 487 | } 488 | ) 489 | ) 490 | ) 491 | .subscribe(); 492 | }, 493 | error: (error: unknown) => { 494 | this.logError( 495 | "Sorry, could not remove this page from Pinboard", 496 | errorMessage(error) 497 | ); 498 | }, 499 | }); 500 | } 501 | return false; 502 | } 503 | 504 | // reset error message 505 | reset(): boolean { 506 | this.error = null; 507 | return false; 508 | } 509 | 510 | // submit form 511 | submit(form: NgForm): boolean { 512 | if (form.valid) { 513 | this.save(form.value as Post); 514 | } 515 | return false; 516 | } 517 | 518 | // save page to Pinboard 519 | save(value: Post): void { 520 | value.url = (value.url || "").trim(); 521 | value.title = (value.title || "").trim(); 522 | if (!value.url || !value.title) { 523 | return; 524 | } 525 | value.description = (value.description || "").trim() || null; 526 | // clean up tags, maximum of 100 tags with 255 chars each 527 | const tags = value.tags 528 | ? value.tags 529 | .split(" ") 530 | .filter((tag) => !!tag) 531 | .slice(0, 100) 532 | .map((tag) => tag.slice(0, 255)) 533 | : []; 534 | value.tags = tags.join(" "); 535 | const savedTags = this.savedTags ? this.savedTags.split(" ") : []; 536 | this.pinboard.save(value).subscribe({ 537 | next: (error: unknown) => { 538 | if (error) { 539 | this.logError( 540 | "Sorry, could not save this page to Pinboard", 541 | errorMessage(error) 542 | ); 543 | } else { 544 | this.pinboard 545 | .updateTagCache(tags, savedTags) 546 | .pipe( 547 | finalize( 548 | () => 549 | void browser.tabs.query({ url: this.url }).then( 550 | (tabs: browser.tabs.Tab[]) => { 551 | for (const tab of tabs) { 552 | this.icon.setIcon(tab.id, true); 553 | } 554 | this.cancel(); 555 | }, 556 | (error: unknown) => { 557 | logError(error); 558 | this.cancel(); 559 | } 560 | ) 561 | ) 562 | ) 563 | .subscribe(); 564 | } 565 | }, 566 | error: (error: unknown) => { 567 | this.logError( 568 | "Sorry, could not save this page to Pinboard", 569 | errorMessage(error) 570 | ); 571 | }, 572 | }); 573 | } 574 | 575 | // save current tabs as tab set to Pinboard 576 | saveTabs(): void { 577 | void browser.tabs 578 | .query({ windowType: "normal", url: "*://*/*" }) 579 | .then((tabs: browser.tabs.Tab[]) => { 580 | const wTabs: Record< 581 | number, 582 | Record 583 | > = {}; 584 | for (const tab of tabs) { 585 | const wId = tab.windowId; 586 | if (!wTabs[wId]) { 587 | wTabs[wId] = {}; 588 | } 589 | wTabs[wId][tab.index] = { 590 | title: tab.title ?? undefined, 591 | url: tab.url ?? undefined, 592 | }; 593 | } 594 | const windows = Object.keys(wTabs).map((wId) => 595 | Object.keys(wTabs[Number(wId)]).map( 596 | (index) => wTabs[Number(wId)][Number(index)] 597 | ) 598 | ); 599 | if (windows.length) { 600 | const data = { 601 | browser: "ffox", 602 | windows: windows, 603 | }; 604 | this.pinboard.saveTabs(data).subscribe({ 605 | next: () => { 606 | this.cancel(); 607 | }, 608 | error: (error: unknown) => { 609 | this.logError( 610 | "Sorry, could not save this tab set to Pinboard.", 611 | errorMessage(error) 612 | ); 613 | }, 614 | }); 615 | } 616 | }); 617 | } 618 | 619 | // navigate to options 620 | settings(): void { 621 | // store a note that we are showing on a popup page 622 | this.storage.setInfo("options.page", "popup"); 623 | void this.router.navigate(["/options"]); 624 | } 625 | 626 | // log out from Pinboard 627 | logOut(): void { 628 | void this.pinboard.forgetToken().subscribe({ 629 | next: () => void this.router.navigate(["/login"]), 630 | }); 631 | } 632 | 633 | // show error message and log it on the console 634 | logError(errmsg: unknown, logmsg: unknown): void { 635 | if (errmsg) { 636 | logError(logmsg ?? errmsg); 637 | } 638 | this.error = errmsg ? errorMessage(errmsg) : null; 639 | this.cdr.detectChanges(); 640 | } 641 | 642 | pinboardLink(page: unknown): boolean { 643 | const pageStr = 644 | typeof page === "string" 645 | ? page 646 | : typeof page === "number" 647 | ? String(page) 648 | : JSON.stringify(page); 649 | if (pageStr && pageStr.includes("~")) { 650 | void this.pinboard.userName.subscribe({ 651 | next: (name: unknown) => { 652 | const nameStr = 653 | typeof name === "string" 654 | ? name 655 | : typeof name === "number" 656 | ? String(name) 657 | : JSON.stringify(name); 658 | this.pinboardLink(pageStr.replace("~", "u:" + nameStr)); 659 | }, 660 | }); 661 | } else { 662 | let url = pinboardPage; 663 | if (pageStr) { 664 | url += pageStr; 665 | } 666 | void browser.tabs.create({ url: url }); 667 | this.cancel(); 668 | } 669 | return false; 670 | } 671 | 672 | // close the whole popup 673 | cancel(): void { 674 | window.close(); 675 | } 676 | } 677 | --------------------------------------------------------------------------------