浏览代码

integrity 属性无需删除,设为空就可以(控制台仍有告警但不影响js执行)

zjcqoo 6 年之前
父节点
当前提交
115f315baf
共有 2 个文件被更改,包括 38 次插入67 次删除
  1. 36 52
      browser/proxy/src/hook.js
  2. 2 15
      browser/proxy/src/page.js

+ 36 - 52
browser/proxy/src/hook.js

@@ -1,24 +1,18 @@
-export const DELETE = {}
 export const RETURN = {}
 
+const {
+  apply,
+  getOwnPropertyDescriptor,
+  defineProperty,
+} = Reflect
+
+const rawMap = new WeakMap()
+
+
 /**
  * @param {Window} win 
  */
 export function createHook(win) {
-  // const {
-  //   Reflect,
-  //   WeakMap,
-  // } = win
-
-  const {
-    apply,
-    getOwnPropertyDescriptor,
-    defineProperty,
-  } = Reflect
-
-  const rawMap = new WeakMap()
-
-
   /**
    * hook function
    * 
@@ -72,9 +66,9 @@ export function createHook(win) {
         return onget.call(this, val)
       },
       setter => function(val) {
-        const ret = onset.call(this, val)
-        if (ret !== null) {
-          val = ret
+        val = onset.call(this, val)
+        if (val == RETURN) {
+          return
         }
         setter.call(this, val)
       }
@@ -97,14 +91,15 @@ export function createHook(win) {
     let keySetMap, keyGetMap
 
     handlers.forEach(v => {
-      // 划线转驼峰('http-equiv' -> 'httpEquiv')
-      const name = v.name.replace(/-(\w)/g, (_, $1) => {
-        return $1.toUpperCase()
-      })
-      hookElemProp(proto, name, v.onget, v.onset)
+      // 带划线的 attr 属性名,转换成驼峰形式的 prop 属性名。
+      // 例如 `http-equiv` -> `httpEquiv`
+      const prop = v.name.replace(/-(\w)/g,
+        (_, char) => char.toUpperCase()
+      )
+      hookElemProp(proto, prop, v.onget, v.onset)
 
       // #text
-      if (name === 'innerText') {
+      if (prop === 'innerText') {
         tagTextHandlerMap[tag] = v
         return
       }
@@ -123,7 +118,7 @@ export function createHook(win) {
         keySetMap = tagKeySetMap[tag]
         keyGetMap = tagKeyGetMap[tag]
       }
-      const key = toLCase.call(name)
+      const key = toLCase.call(v.name)
       keySetMap[key] = v.onset
       keyGetMap[key] = v.onget
       hasAttr = true
@@ -133,33 +128,30 @@ export function createHook(win) {
       return
     }
 
-    // 如果之前调用过 setAttribute,那么直接返回上次设置的值。
-    // 如果没有调用过则回 onget
+    // 如果之前调用过 setAttribute,直接返回上次设置的值;
+    // 如果没有调用过回 onget 的回调值。
     func(proto, 'getAttribute', oldFn => function(name) {
       const key = toLCase.call(name)
-      const get = keyGetMap[key]
-      if (get) {
-        const lastVal = this['_k' + key]
-        if (lastVal !== undefined) {
-          return lastVal
-        }
-        const val = apply(oldFn, this, arguments)
-        return get(val)
+      const onget = keyGetMap[key]
+      if (!onget) {
+        return apply(oldFn, this, arguments)
       }
-      return apply(oldFn, this, arguments)
+
+      const lastVal = this['_k' + key]
+      if (lastVal !== undefined) {
+        return lastVal
+      }
+      const val = apply(oldFn, this, arguments)
+      return onget(val)
     })
 
     func(proto, 'setAttribute', oldFn => function(name, val) {
       const key = toLCase.call(name)
-      const set = keySetMap[key]
-      if (set) {
+      const onset = keySetMap[key]
+      if (onset) {
         this['_k' + key] = val
 
-        const ret = set.call(this, val)
-        if (ret === DELETE) {
-          this.removeAttribute(key)
-          return
-        }
+        const ret = onset.call(this, val)
         if (ret === RETURN) {
           return
         }
@@ -180,10 +172,6 @@ export function createHook(win) {
   function parseNewTextNode(node, handler, elem) {
     const val = node.nodeValue
     const ret = handler.onset.call(elem, val)
-    if (ret === DELETE) {
-      node.remove()
-      return
-    }
     if (ret === RETURN) {
       return
     }
@@ -201,10 +189,6 @@ export function createHook(win) {
     }
     const val = rawGetAttr.call(elem, name)
     const ret = handler.onset.call(elem, val)
-    if (ret === DELETE) {
-      elem.removeAttribute(name)
-      return
-    }
     if (ret === RETURN) {
       return
     }
@@ -251,7 +235,7 @@ export function createHook(win) {
   // })
 
   // hide source code
-  func(Function.prototype, 'toString', oldFn => function() {
+  func(win.Function.prototype, 'toString', oldFn => function() {
     return apply(oldFn, rawMap.get(this) || this, arguments)
   })
   

+ 2 - 15
browser/proxy/src/page.js

@@ -1,4 +1,4 @@
-import {createHook, DELETE} from './hook.js'
+import {createHook} from './hook.js'
 import * as urlx from './urlx.js'
 import * as util from './util.js'
 import * as nav from './nav.js'
@@ -321,21 +321,9 @@ function initWin(win) {
     },
     onset(val) {
       this._integrity = val
-      return DELETE
+      return ''
     }
   },
-  // // 
-  // {
-  //   name: 'type',
-  //   onget(val) {
-  //     return val
-  //   },
-  //   onset(val) {
-  //     updateScript(this)
-  //     return val
-  //   }
-  // },
-  // 
   {
     name: 'innerText',
     onget(val) {
@@ -363,7 +351,6 @@ function initWin(win) {
       setter.call(this, val)
     }
   }
-  hook.prop(scriptProto, 'innerHTML', scriptGetJs, scriptSetJs)
   hook.prop(scriptProto, 'text', scriptGetJs, scriptSetJs)
 
   const JS_MIME = {