search

Home  >  Q&A  >  body text

javascript - 如何使用Scriptish向火狐添加innerText属性?

首先,是学校的教务网页使用了innerText属性,所以我想自己写一个UserScript脚本来实现innerText属性。
其实,只要执行如下脚本后,就有了innerText属性了:

if (!!document.getBoxObjectFor || window.mozInnerScreenX != null) {
    HTMLElement.prototype.__defineGetter__("innerText", function() {
      return this.textContent;
    });
  HTMLElement.prototype.__defineSetter__("innerText", function (value) {
    this.textContent = value;
  });
}

但是,我自己写了脚本之后,仍然无法设置innerText属性,提示TypeError: HTMLElement.prototype.__defineGetter__ is not a function
请问如何解决?
另外,innerText属性在onload代码中就使用了。
我觉得Scriptish的权限控制也是需要考虑的。


附上我写的测试脚本:

// ==UserScript==
// @name           Test
// @namespace      www.example.com
// @include        *
// @version        1
// ==/UserScript==

if (!!document.getBoxObjectFor || window.mozInnerScreenX != null) {
    HTMLElement.prototype.__defineGetter__("innerText", function() {
      return this.textContent;
    });
  HTMLElement.prototype.__defineSetter__("innerText", function (value) {
    this.textContent = value;
  });
}
怪我咯怪我咯2896 days ago749

reply all(3)I'll reply

  • 阿神

    阿神2017-04-10 13:13:15

    Greasemonkey/Scriptish 上的使用者腳本在 sandbox 裡執行,有些操作會被擋下來,你可以用 unsafeWindow 或 location hack 繞過沙箱。

    unsafeWindow 相當於 window.wrappedJSObject,HTMLElement.prototype 在 GM 裡要寫成 HTMLElement.wrappedJSObject.prototype

    引用自 http://forum.moztw.org/viewtopic.php?t=33679

    所以,我最后的代码是这样的:


    // ==UserScript== // @name Test // @namespace www.example.com // @include * // @version 1 // ==/UserScript== Object.defineProperty(HTMLElement.wrappedJSObject.prototype, 'innerText', { get: function(){ return this.textContent; }, set: function(val) { this.textContent = val; } })

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 13:13:15

    不是权限的原因,而是它本身就没有那个 __defineSetter__ 属性。

    Object.defineProperty(HTMLElement.prototype, 'innerText', {
      get: function(){
        return this.textContent;
      },
      set: function(val) {
        this.textContent = val;
      }
    })
    

    Firebug 控制台测试可行。文档

    reply
    0
  • 迷茫

    迷茫2017-04-10 13:13:15

    我不是很确定原因……
    你可以试试这样执行:

    (function(){
    /** 
    * 
    *  Base64 encode / decode 
    * 
    *  @author haitao.tu 
    *  @date   2010-04-26 
    *  @email  tuhaitao@foxmail.com 
    * 
    */  
    
    function Base64() {  
        // private property  
        _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
    
        // public method for encoding  
        this.encode = function (input) {  
            var output = "";  
            var chr1, chr2, chr3, enc1, enc2, enc3, enc4;  
            var i = 0;  
            input = _utf8_encode(input);  
            while (i < input.length) {  
                chr1 = input.charCodeAt(i++);  
                chr2 = input.charCodeAt(i++);  
                chr3 = input.charCodeAt(i++);  
                enc1 = chr1 >> 2;  
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
                enc4 = chr3 & 63;  
                if (isNaN(chr2)) {  
                    enc3 = enc4 = 64;  
                } else if (isNaN(chr3)) {  
                    enc4 = 64;  
                }  
                output = output +  
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +  
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);  
            }  
            return output;  
        }  
    
        // public method for decoding  
        this.decode = function (input) {  
            var output = "";  
            var chr1, chr2, chr3;  
            var enc1, enc2, enc3, enc4;  
            var i = 0;  
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");  
            while (i < input.length) {  
                enc1 = _keyStr.indexOf(input.charAt(i++));  
                enc2 = _keyStr.indexOf(input.charAt(i++));  
                enc3 = _keyStr.indexOf(input.charAt(i++));  
                enc4 = _keyStr.indexOf(input.charAt(i++));  
                chr1 = (enc1 << 2) | (enc2 >> 4);  
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  
                chr3 = ((enc3 & 3) << 6) | enc4;  
                output = output + String.fromCharCode(chr1);  
                if (enc3 != 64) {  
                    output = output + String.fromCharCode(chr2);  
                }  
                if (enc4 != 64) {  
                    output = output + String.fromCharCode(chr3);  
                }  
            }  
            output = _utf8_decode(output);  
            return output;  
        }  
    
        // private method for UTF-8 encoding  
        _utf8_encode = function (string) {  
            string = string.replace(/\r\n/g,"\n");  
            var utftext = "";  
            for (var n = 0; n < string.length; n++) {  
                var c = string.charCodeAt(n);  
                if (c < 128) {  
                    utftext += String.fromCharCode(c);  
                } else if((c > 127) && (c < 2048)) {  
                    utftext += String.fromCharCode((c >> 6) | 192);  
                    utftext += String.fromCharCode((c & 63) | 128);  
                } else {  
                    utftext += String.fromCharCode((c >> 12) | 224);  
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
                    utftext += String.fromCharCode((c & 63) | 128);  
                }  
    
            }  
            return utftext;  
        }  
    
        // private method for UTF-8 decoding  
        _utf8_decode = function (utftext) {  
            var string = "";  
            var i = 0;  
            var c = c1 = c2 = 0;  
            while ( i < utftext.length ) {  
                c = utftext.charCodeAt(i);  
                if (c < 128) {  
                    string += String.fromCharCode(c);  
                    i++;  
                } else if((c > 191) && (c < 224)) {  
                    c2 = utftext.charCodeAt(i+1);  
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
                    i += 2;  
                } else {  
                    c2 = utftext.charCodeAt(i+1);  
                    c3 = utftext.charCodeAt(i+2);  
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
                    i += 3;  
                }  
            }  
            return string;  
        }  
    }  
    function save(key, content){
        if(window.localStorage){
            localStorage.setItem('us.' + key, content);
        };
    };
    var base64 = new Base64();
    var code = "aWYgKCEhZG9jdW1lbnQuZ2V0Qm94T2JqZWN0Rm9yIHx8IHdpbmRvdy5tb3pJbm5lclNjcmVlblggIT0gbnVsbCkgewogICAgSFRNTEVsZW1lbnQucHJvdG90eXBlLl9fZGVmaW5lR2V0dGVyX18oImlubmVyVGV4dCIsIGZ1bmN0aW9uKCkgewogICAgICByZXR1cm4gdGhpcy50ZXh0Q29udGVudDsKICAgIH0pOwogIEhUTUxFbGVtZW50LnByb3RvdHlwZS5fX2RlZmluZVNldHRlcl9fKCJpbm5lclRleHQiLCBmdW5jdGlvbiAodmFsdWUpIHsKICAgIHRoaXMudGV4dENvbnRlbnQgPSB2YWx1ZTsKICB9KTsKfQ==";
    var script = base64.decode(code);
    save("scriptINNER",script);
    location.assign("javascript:eval(localStorage.getItem('us.scriptINNER'));void(0);");
    })();
    

    这是我尝试出的在所有可以用userscript的浏览器中,执行js权限最高的方法……如果这个不行的话,我觉不知到了。。

    reply
    0
  • Cancelreply