Browse Source

refactor: deduplicate some safe methods

tophf 5 years ago
parent
commit
68e0a0c3a6

+ 1 - 1
src/common/util.js

@@ -110,7 +110,7 @@ export function formatTime(duration) {
 }
 
 // used in an unsafe context so we need to save the original functions
-const { hasOwnProperty } = Object.prototype;
+export const { hasOwnProperty } = Object.prototype;
 export function isEmpty(obj) {
   for (const key in obj) {
     if (obj::hasOwnProperty(key)) {

+ 3 - 2
src/injected/content/clipboard.js

@@ -1,5 +1,7 @@
 import { sendCmd } from '../utils';
-import { addEventListener, describeProperty, logging } from '../utils/helpers';
+import {
+  addEventListener, describeProperty, logging, removeEventListener,
+} from '../utils/helpers';
 import bridge from './bridge';
 
 // old Firefox defines it on a different prototype so we'll just grab it from document directly
@@ -7,7 +9,6 @@ const { execCommand } = document;
 const { setData } = DataTransfer.prototype;
 const { get: getClipboardData } = describeProperty(ClipboardEvent.prototype, 'clipboardData');
 const { preventDefault, stopImmediatePropagation } = Event.prototype;
-const { removeEventListener } = EventTarget.prototype;
 
 let clipboardData;
 

+ 1 - 3
src/injected/content/inject.js

@@ -9,7 +9,7 @@ import { sendCmd } from '#/common';
 
 import {
   forEach, join, append, createElementNS, defineProperty, NS_HTML,
-  charCodeAt, fromCharCode,
+  charCodeAt, fromCharCode, replace, remove,
 } from '../utils/helpers';
 import bridge from './bridge';
 
@@ -20,8 +20,6 @@ const VMInitInjection = window[Symbol.for(process.env.INIT_FUNC_NAME)];
 defineProperty(window, Symbol.for(process.env.INIT_FUNC_NAME), { value: 1 });
 
 const { encodeURIComponent } = global;
-const { replace } = String.prototype;
-const { remove } = Element.prototype;
 
 bridge.addHandlers({
   Inject: injectScript,

+ 6 - 5
src/injected/utils/helpers.js

@@ -3,7 +3,7 @@
 // eslint-disable-next-line no-restricted-properties
 export const {
   // types
-  Blob, Boolean, Error, Promise, Uint8Array,
+  Boolean, Error, Promise, Uint8Array,
   // props and methods
   atob, isFinite, setTimeout,
 } = global;
@@ -15,15 +15,16 @@ export const {
 
 export const {
   keys: objectKeys, values: objectValues, entries: objectEntries,
-  assign, defineProperty, defineProperties, getOwnPropertyDescriptor: describeProperty,
+  assign, defineProperty, getOwnPropertyDescriptor: describeProperty,
 } = Object;
-export const { charCodeAt, match, slice } = String.prototype;
+export const {
+  charCodeAt, match, slice, replace,
+} = String.prototype;
 export const { toString: objectToString } = Object.prototype;
 const { toString: numberToString } = Number.prototype;
-const { replace } = String.prototype;
 export const { fromCharCode } = String;
 export const { addEventListener, removeEventListener } = EventTarget.prototype;
-export const { append, setAttribute } = Element.prototype;
+export const { append, remove, setAttribute } = Element.prototype;
 export const { createElementNS } = Document.prototype;
 export const logging = assign({}, console);
 

+ 3 - 3
src/injected/web/gm-wrapper.js

@@ -1,8 +1,9 @@
+import { hasOwnProperty as has } from '#/common';
 import { INJECT_CONTENT, METABLOCK_RE } from '#/common/consts';
 import bridge from './bridge';
 import {
   concat, filter, forEach, includes, indexOf, map, match, push, slice,
-  defineProperty, describeProperty, objectKeys,
+  defineProperty, describeProperty, objectKeys, replace,
   addEventListener, removeEventListener,
 } from '../utils/helpers';
 import { makeGmApi } from './gm-api';
@@ -10,8 +11,7 @@ import { makeGmApi } from './gm-api';
 const { Proxy } = global;
 const { getOwnPropertyNames, getOwnPropertySymbols } = Object;
 const { splice } = Array.prototype;
-const { hasOwnProperty: has } = Object.prototype;
-const { replace, startsWith } = String.prototype;
+const { startsWith } = String.prototype;
 
 let gmApi;
 let gm4Api;

+ 1 - 1
src/injected/web/load-scripts.js

@@ -2,6 +2,7 @@ import { getUniqId } from '#/common';
 import { INJECT_CONTENT } from '#/common/consts';
 import {
   filter, map, defineProperty, describeProperty, Boolean, Promise, setTimeout, log, noop,
+  remove,
 } from '../utils/helpers';
 import bridge from './bridge';
 import store from './store';
@@ -10,7 +11,6 @@ import { deletePropsCache, wrapGM } from './gm-wrapper';
 const { concat } = Array.prototype;
 const { document } = global;
 const { get: getCurrentScript } = describeProperty(Document.prototype, 'currentScript');
-const { remove } = Element.prototype;
 
 bridge.addHandlers({
   LoadScripts(data) {