2012-11-14 49 views
2

我想打一个Chrome扩展程序,会抢了源代码是这样的:编辑页面源代码与谷歌Chrome扩展

document.getElementsByTagName('html')[0].innerHTML 

这可能不是,虽然最好的办法......然后更改搜索和替换来源中的文本......就像页面中的“cool”这个词一样,然后用别的东西代替它,并将修改后的页面显示给用户。我将如何做到这一点?

此外,不仅仅是文本,我需要编辑源代码,而不仅仅是文本显示。

我的目标是编辑页面源链接,例如样式表和其他来源。我需要更改https:// - 如果是一个 - 并将其替换为http://

+0

你就不能运行在用户的浏览器的JavaScript?并对其进行修改 – Andy

+1

在显示网页之前,我可以进行查找和替换吗?我的目标是编辑页面源链接,例如样式表和其他来源。我需要更改Https://如果是一个,并将其替换为Http:// –

回答

0

最好的办法是:

  1. 添加了jQuery你options.html
  2. 比使用jQuery解决像

    $( '#searchfield')你的问题VAL(“TEST “); //依此类推......

希望它有帮助。

PS:我写了一个镀铬的扩展,以及也许它可以帮助你开始(https://github.com/marcofrancke/AmazonExtended)

+0

因此,我需要替换源中url的https,然后将其替换为http。在加载页面之前,所以链接不会是https,而是http。 –

0

尝试http://api.jquery.com/clone/var element = $0;(返回根元素)

例如: 代码来获取标签名称说明:

var getElementReference = function() { 

    var element = $0; 
    var tagName = element.tagName; 
    var dictionary = { 
     a: "Defines an anchor", 
     abbr: "Defines an abbreviation", 
     acronym: "Defines an acronym", 
     address: "Defines contact information for the authorowner of a document", 
     applet: "Deprecated. Defines an embedded applet", 
     area: " Defines an area inside an image-map", 
     b: "Defines bold text", 
     base: " Defines a default address or a default target for all links on a page", 
     basefont: " Deprecated. Defines a default font, color, or size for the text in a page", 
     bdo: "Defines the text direction", 
     big: "Defines big text", 
     blockquote: "Defines a long quotation", 
     body: "Defines the document's body", 
     br: " Defines a single line break", 
     button: "Defines a push button", 
     caption: "Defines a table caption", 
     center: "Deprecated. Defines centered text", 
     cite: "Defines a citation", 
     code: "Defines computer code text", 
     col: " Defines attribute values for one or more columns in a table ", 
     colgroup: "Defines a group of columns in a table for formatting", 
     dd: "Defines a description of a term in a definition list", 
     del: "Defines deleted text", 
     dfn: "Defines a definition term", 
     dir: "Deprecated. Defines a directory list", 
     div: "Defines a section in a document", 
     dl: "Defines a definition list", 
     dt: "Defines a term (an item) in a definition list", 
     em: "Defines emphasized text ", 
     fieldset: "Defines a border around elements in a form", 
     font: "Deprecated. Defines font, color, and size for text", 
     form: "Defines an HTML form for user input", 
     frame: " Defines a window (a frame) in a frameset", 
     frameset: "Defines a set of frames", 
     h1: " Defines HTML headings", 
     h2: " Defines HTML headings", 
     h3: " Defines HTML headings", 
     h4: " Defines HTML headings", 
     h5: " Defines HTML headings", 
     h6: " Defines HTML headings", 
     head: "Defines information about the document", 
     hr: " Defines a horizontal line", 
     html: "Defines an HTML document", 
     i: "Defines italic text", 
     iframe: "Defines an inline frame", 
     img: " Defines an image", 
     input: " Defines an input control", 
     ins: "Defines inserted text", 
     isindex: "Deprecated. Defines a searchable index related to a document", 
     kbd: "Defines keyboard text", 
     label: "Defines a label for an input element", 
     legend: "Defines a caption for a fieldset element", 
     li: "Defines a list item", 
     link: " Defines the relationship between a document and an external resource", 
     map: "Defines an image-map ", 
     menu: "Deprecated. Defines a menu list", 
     meta: " Defines metadata about an HTML document", 
     noframes: "Defines an alternate content for users that do not support frames", 
     noscript: "Defines an alternate content for users that do not support client-side scripts", 
     object: "Defines an embedded object", 
     ol: "Defines an ordered list", 
     optgroup: "Defines a group of related options in a select list", 
     option: "Defines an option in a select list", 
     p: "Defines a paragraph", 
     param: " Defines a parameter for an object", 
     pre: "Defines preformatted text", 
     q: "Defines a short quotation", 
     s: "Deprecated. Defines strikethrough text", 
     samp: "Defines sample computer code", 
     script: "Defines a client-side script", 
     select: "Defines a select list (drop-down list)", 
     small: "Defines small text", 
     span: "Defines a section in a document", 
     strike: "Deprecated. Defines strikethrough text", 
     strong: "Defines strong text", 
     style: "Defines style information for a document", 
     sub: "Defines subscripted text", 
     sup: "Defines superscripted text", 
     table: "Defines a table", 
     tbody: "Groups the body content in a table", 
     td: "Defines a cell in a table", 
     textarea: "Defines a multi-line text input control", 
     tfoot: "Groups the footer content in a table", 
     th: "Defines a header cell in a table", 
     thead: "Groups the header content in a table", 
     title: "Defines the title of a document", 
     tr: "Defines a row in a table", 
     tt: "Defines teletype text", 
     u: "Deprecated. Defines underlined text", 
     ul: "Defines an unordered list", 
     xmp: "Deprecated. Defines preformatted text" 
    }; 

    var result = { 
     tag: tagName, 
     description: dictionary[tagName.toLowerCase()], 
     __proto__: null 
    }; 

    return result; 
} 
+0

克隆为什么会有帮助?我需要在源代码链接中找到https://,然后将其更改为http://?我没有跟着你。 –

+0

您可以尝试使用chrome.devtools.inspectedWindow.getResources API并使用resource.setContent编辑URL – Sudarshan