{"record":{"id":"2fa0883c713485c6","repo":"mozilla/pdf.js","slug":"doc-selectedannots-is-read-only","errorCode":null,"errorMessage":"doc.selectedAnnots is read-only","messagePattern":"doc\\.selectedAnnots is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":631,"sourceCode":"\n  set requiresFullSave(_) {\n    throw new Error(\"doc.requiresFullSave is read-only\");\n  }\n\n  get securityHandler() {\n    return this._securityHandler;\n  }\n\n  set securityHandler(_) {\n    throw new Error(\"doc.securityHandler is read-only\");\n  }\n\n  get selectedAnnots() {\n    return [];\n  }\n\n  set selectedAnnots(_) {\n    throw new Error(\"doc.selectedAnnots is read-only\");\n  }\n\n  get sounds() {\n    return [];\n  }\n\n  set sounds(_) {\n    throw new Error(\"doc.sounds is read-only\");\n  }\n\n  get spellDictionaryOrder() {\n    return this._spellDictionaryOrder;\n  }\n\n  set spellDictionaryOrder(spellDictionaryOrder) {\n    this._spellDictionaryOrder = spellDictionaryOrder;\n  }\n","sourceCodeStart":613,"sourceCodeEnd":649,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L613-L649","documentation":"`Doc.selectedAnnots` returns the array of currently selected annotations; PDF.js returns `[]` because the scripting sandbox does not expose annotation selection state. It is read-only per the Acrobat spec — selection is driven by user input/UI, not settable programmatically through this property. The throwing setter enforces that.","triggerScenarios":"A script writes `doc.selectedAnnots = [...];` to force a selection. The setter throws on assignment.","commonSituations":"Annotation workflows ported from Acrobat; code that assumes selection is settable; scripts that batch-edit annotations.","solutions":["Remove the assignment; read the value (`var a = doc.selectedAnnots;`, `[]` in PDF.js).","Drive annotation selection through the viewer's annotation layer APIs, not the scripting `Doc` object.","Run legacy scripts under try/catch."],"exampleFix":"// before\ndoc.selectedAnnots = [annot1, annot2];\n\n// after\nvar sel = doc.selectedAnnots; // read-only; [] in PDF.js","handlingStrategy":"try-catch","validationCode":"const READ_ONLY_DOC_PROPS = new Set(['selectedAnnots','sounds' /* ... */]);\nfunction assertsNoReadOnlyWrite(scriptSrc) {\n  for (const p of READ_ONLY_DOC_PROPS) {\n    if (new RegExp(`\\\\bdoc\\\\.${p}\\\\s*=[^=]`).test(scriptSrc)) {\n      throw new Error(`Script writes read-only property doc.${p}`);\n    }\n  }\n}\nassertsNoReadOnlyWrite(myScript);","typeGuard":"function isReadOnlyDocProp(doc, prop) {\n  const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)\n    || Object.getOwnPropertyDescriptor(doc, prop);\n  return !!desc && !!desc.get && !desc.set;\n}\nisReadOnlyDocProp(doc, 'selectedAnnots'); // true","tryCatchPattern":"try {\n  doc.selectedAnnots = [annot];\n} catch (e) {\n  if (/selectedAnnots is read-only/.test(e.message)) console.warn(e.message);\n  else throw e;\n}","preventionTips":["Annotation selection is UI-driven; use the viewer's annotation layer, not the Doc scripting object.","Lint scripts for assignments to read-only collection properties.","Wrap annotation-workflow scripts in try/catch."],"tags":["scripting","acrobat-js-api","doc-object","read-only","annotations"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}