MozTW 討論區 https://forum.moztw.org/ |
|
怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype https://forum.moztw.org/viewtopic.php?f=11&t=33679 |
第 1 頁 (共 1 頁) |
發表人: | 訪客 [ 2011-10-04, 15:51 ] |
文章主題 : | 怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype |
怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype呢? 我試了幾下他都說是undefined,例如 HTMLElement.prototype 但是在網址列倒是可以存取(找的到) 所以我直覺就是權限問題(?).... PS.我主要是想要弄一個outerHTML的功能 |
發表人: | shyangs [ 2011-10-04, 22:54 ] |
文章主題 : | Re: 怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype |
Greasemonkey/Scriptish 上的使用者腳本在 sandbox 裡執行,有些操作會被擋下來,你可以用 unsafeWindow 或 location hack 繞過沙箱。參考 Extending the DOM。 |
發表人: | 訪客 [ 2011-10-06, 16:01 ] |
文章主題 : | Re: 怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype |
多謝大大指點 經測試,unsafeWindow還是不行....不知道為什麼?? 至於另一個方法還沒試...(其實我不喜歡透過location的方式...) 說起來....沙盒這樣的設計真的有比較安全嗎?... 小弟才疏學淺實在搞不懂.... |
發表人: | shyangs [ 2011-10-06, 19:25 ] |
文章主題 : | Re: 怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype |
unsafeWindow 相當於 window.wrappedJSObject,HTMLElement.prototype 在 GM 裡要寫成 HTMLElement.wrappedJSObject.prototype 以下參考: https://gist.github.com/781650 代碼: // ==UserScript== // @author think49 // @name outerHTML.js // @description outerHTML.js : HTML5 規定の element.outerHTML を定義する。 // @namespace https://gist.github.com/781650 // @version 1.4 // @include * // ==/UserScript== /** * outerHTML.js * * @version 1.4 * @author think49 */ if (!('outerHTML' in document.createElement('p')) && 'innerHTML' in document.createElement('p') && (typeof HTMLElement === 'function' || typeof HTMLElement === 'object')) { (function () { var _Node = (typeof Node === 'function' || typeof Node === 'object') ? Node : { ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5, ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12 }; function getOuterHTML () { var node; node = this.ownerDocument.createElement('body'); node.appendChild(this.cloneNode(true)); return node.innerHTML; } function setOuterHTML (htmlString) { var doc, parentNode, node, df; parentNode = this.parentNode; if (!parentNode) { // HTML5 3.5.6 (step2) return; } if (parentNode.nodeType === _Node.DOCUMENT_NODE) { // HTML5 3.5.6 (step3) throw new Error('NO_MODIFICATION_ALLOWED_ERR'); } doc = this.ownerDocument; node = doc.createElement('body'); node.innerHTML = htmlString; node = node.firstChild; df = doc.createDocumentFragment(); while (node) { df.appendChild(node); node = node.nextSibling; } parentNode.replaceChild(df, this); } if ('defineProperty' in Object) { // ECMAScript 5 Object.defineProperty(this, 'outerHTML', {get: getOuterHTML, set: setOuterHTML}); return; } if ('__defineGetter__' in this && '__defineSetter__' in this) { // for Firefox this.__defineGetter__('outerHTML', getOuterHTML); this.__defineSetter__('outerHTML', setOuterHTML); } }).call(HTMLElement.wrappedJSObject.prototype); } /* 以本頁面演示 alert(document.getElementById('pagecontent').wrappedJSObject.outerHTML); document.getElementById('pagecontent').wrappedJSObject.outerHTML="<div>修改</div>"; */ 最後我加了一段用本頁面 ID="pagecontent" 的元素的演示,你可以取消註解玩看看。 |
發表人: | 訪客 [ 2011-10-07, 18:31 ] |
文章主題 : | Re: 怎樣在 Greasemonkey 和 scriptish 上存取js核心成員的prototype |
感謝shyangs大相助 十分有用,獲益良多呢! 再來研究研究... |
第 1 頁 (共 1 頁) | 所有顯示的時間為 UTC + 8 小時 |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |