Browse Source

fix: new script without `@name`

fixes #1372
tophf 4 years ago
parent
commit
30814db7db

+ 0 - 1
src/_locales/en/messages.yml

@@ -389,7 +389,6 @@ labelNoFrames:
 labelNoName:
   description: Text as the name of a script when no @name is assigned.
   message: No Name
-  touched: false
 labelNoSearchScripts:
   description: Message shown when no script is found in search results.
   message: No script is found.

+ 1 - 1
src/background/utils/db.js

@@ -458,7 +458,7 @@ export async function updateScriptInfo(id, data) {
 /** @return {Promise<{ isNew?, update, where }>} */
 export async function parseScript(src) {
   const meta = parseMeta(src.code);
-  if (!meta.name) throw i18n('msgInvalidScript');
+  if (!meta.name) throw `${i18n('msgInvalidScript')}\n${i18n('labelNoName')}`;
   const result = {
     update: {
       message: src.message == null ? i18n('msgUpdated') : src.message || '',

+ 1 - 0
src/background/utils/script.js

@@ -110,6 +110,7 @@ export function newScript(data) {
       shouldUpdate: 1,
     },
     meta: parseMeta(code),
+    props: {},
   };
   return { script, code };
 }

+ 2 - 1
src/common/browser.js

@@ -62,7 +62,8 @@ if (!global.browser?.runtime?.sendMessage) {
         }
         // Prefer `reject` over `throw` which stops debugger in 'pause on exceptions' mode
         if (err) {
-          err = new Error(`${err}\n${stackInfo.stack}`);
+          // Using \n\n so the calling code can strip the stack easily if needed
+          err = new Error(`${err}\n\n${stackInfo.stack}`);
           err.isRuntime = isRuntime;
           reject(err);
         }

+ 3 - 2
src/common/index.js

@@ -4,7 +4,7 @@
 
 import { browser } from '#/common/consts';
 import { deepCopy } from './object';
-import { noop } from './util';
+import { i18n, noop } from './util';
 
 export { normalizeKeys } from './object';
 export * from './util';
@@ -131,7 +131,8 @@ export function getLocaleString(meta, key) {
 }
 
 export function getScriptName(script) {
-  return script.custom.name || getLocaleString(script.meta, 'name') || `#${script.props.id}`;
+  return script.custom.name || getLocaleString(script.meta, 'name')
+    || `#${script.props.id ?? i18n('labelNoName')}`;
 }
 
 export function getFullUrl(url, base) {

+ 2 - 1
src/options/views/edit/index.vue

@@ -297,7 +297,8 @@ export default {
         this.canSave = false; // ...and set it explicitly in case codeDirty was false
         if (res?.where?.id) this.script = res.update;
       } catch (err) {
-        showMessage({ text: err });
+        // Stripping stack info added by browser.js
+        showMessage({ text: `${err.message || err}`.split('\n\n')[0] });
       }
     },
     close(cm) {