简体   繁体   English

Chrome扩展程序在内容页面内加载HTML页面

[英]Chrome Extension load HTML page inside content page

Hey all I am trying to load an HTML page within the content part of the page. 嘿,我正在尝试将HTML页面加载到页面内容部分。 The HTML page is a drag and drop type of box that I want to float around the content page. HTML页面是一种拖放类型的框,我想在内容页面中浮动。 This page allows the user to move the box anywhere around the area they wish. 该页面允许用户将框移动到所需区域的任何位置。

However, I am unable to find any good examples of how to inject my HTML page within the content page? 但是,我找不到如何在内容页面中注入HTML页面的任何好的示例? The HTML page does have calls to load CSS and JS as well. HTML页面确实具有调用以加载CSS和JS。

I can do the following: 我可以执行以下操作:

$('body').prepend(HTMLcodeHere);

And it does load into the content page but like I said above, I have css and js that need to also be loaded when that page loads so doing it that way would not work out. 而且确实可以加载到内容页面中,但是就像我在上面说的那样,我有css和js在加载该页面时也需要加载,因此这样做是不可能的。

Example of a free floting window : 自由浮动窗口的示例:

在此处输入图片说明

I've found a few extension examples that have those type of free floating windows but either the code behind it is packed or I just dont plain understand how they did it. 我发现了一些具有这些类型的自由浮动窗口的扩展示例,但是它们背后的代码很紧凑,或者我只是不明白它们是如何实现的。

So any help would be great! 因此,任何帮助都会很棒!

Manifest: 表现:

content_scripts": [ {
      "css": [],
      "js": ["js/jquery.js"],
      "matches": ["<all_urls>"],
      "run_at": "document_start"
 }],

"permissions": [
    "background", 
    "notifications", 
    "contextMenus",
    "tabs",
    "storage",
    "unlimitedStorage",
    "activeTab",
    "<all_urls>",
    "http://*/*", 
    "bookmarks",
    "https://*/*"
 ],
"web_accessible_resources": ["img/*.*", "js/*.*", "css/*.*", "*.*", "html/*.*"]

And I am loading up the page like so: 我正在像这样加载页面:

document.getElementById('lfMn')
                .insertAdjacentHTML('afterend',
                    '<link href="chrome-extension://' + applicationID + '/css/codemirror.css" rel="stylesheet" type="text/css" />' + 
                    '<link href="chrome-extension://' + applicationID + '/js/beautify.js" type="text/javascript" />' + 
                    '<link href="chrome-extension://' + applicationID + '/js/packerStuff.js" type="text/javascript" />' +                       
                    '<script type="text/javascript">' + 
                        'window.onload = function(e){' +
                            'Modal.init();' +
                            'Modal.open();' +
                            'alert("hello");' +
                            'console.log(\'loadedhere\');' +
                        '};' +
                    '</script>' +
                    '<button type="button" id="whitespaces" name="whitespaces" onClick="Modal.open();">module</button>' +
                    '<!-- modal -->' +
                    '<div class="modal">' +
                        '<header class="modal-header">' +
                            '<h1 class="modal-header-title left">Settings Code</h1>' +         
                            '<button class="modal-header-btn2 right" id="saveCode" name="saveCode" data-tipso="Save current code">Save Code</button>' +
                            '<button class="modal-header-btn right" id="cleanCode" name="cleanCode" data-tipso="Clean current code">Clean Code</button>' +
                            '<button class="modal-header-btn3 right modal-close" id="close" name="close" data-tipso="Close Box">Close</button>' +
                            '<button class="modal-header-btn5 right" id="dockR" name="dockR" data-tipso="Dock Right"> </button>' +
                            '<button class="modal-header-btn4 right" id="dockL" name="dockL" data-tipso="Dock Left"> </button>' +
                            '<button class="modal-header-btn6 right" id="dockF" name="dockF" data-tipso="Dock Fullscreen"> </button>' +
                            '<button class="modal-header-btn7 right" id="dockN" name="dockN" data-tipso="Dock Normal"> </button>' +
                            '<button class="modal-header-btn8 right" id="searchIt" name="searchIt" data-tipso="Seach thru code">Search</button>' +
                            '<fieldset class="searchField" id="thesearch">' +
                                '<div class="input">' +
                                    '<input type="text" name="s" id="s" value="Enter your search" />' +
                                '</div>' +
                                '<input type="button" id="searchSubmit" value="" />' +
                            '</fieldset>' +
                        '</header>' +
                        '<div class="modal-body">' +
                            '<section class="modal-content">' +
                                '<!-- MAIN CODE AREA -->' +
                                '<div style="line-height: 0;" id="mainDiv">' +
                                    '<textarea id="source" rows="30" cols="160">' +
                                        'function change() {' +                             
                                            'var myNewTitle = document.getElementById(\'myTextField\').value;' +
                                            'if (myNewTitle.length == 0) {' +
                                                'alert(\'Write Some real Text please.\');' +
                                                'return;' +
                                            '}' +                                       
                                            'var title = document.getElementById(\'title\');' +
                                            'title.innerHTML = myNewTitle;' +                               
                                        '}' +
                                    '</textarea>' +
                                '</div>' +
                                '<!-- MAIN CODE AREA -->' +
                            '</section>' +
                        '</div>' +
                    '</div>' +
                    '');

The only thing i see on the page is the "module" button. 我在页面上唯一看到的是“模块”按钮。 and it never executes the alert("hello"); 而且它从不执行alert(“ hello”); nor the console.log(\\'loadedhere\\'); 也没有console.log(\\'loadedhere \\'); in the window.onload = function(e){ script.... window.onload = function(e){脚本....

There is no right or wrong way to do this, but your your html has to be dynamically added, so using $('body').prepend() is fine. 没有正确或错误的方法,但是您的html必须动态添加,因此使用$('body').prepend()很好。

for the css and js you can use content scripts as so: 对于CSS和JS,您可以这样使用内容脚本:

"content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "css": ["mystyles.css"],
      "js": ["jquery.js", "myscript.js"]
    }
  ],

optionally, you can use web accessible resources to allow you to inject your css(or js) into the page like so: $('head').append('<link href="<chrome-extension://ext id/mycssfile.css" rel="stylesheet" type="text/css" />') . (可选)您可以使用可访问网络的资源来将css(或js)注入页面,如下所示: $('head').append('<link href="<chrome-extension://ext id/mycssfile.css" rel="stylesheet" type="text/css" />')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM