How to insert HTML/JS into window (of type “panel”) created by chrome.windows.create?

痴心易碎 提交于 2019-12-23 18:11:48

问题


I'm creating a window in a Chrome Extension using chrome.windows.create. It's of type panel, so it's not a regular browser window or tab, but a free-standing window floating above all others. Having created it, I now need to insert some HTML and JavaScript into it. So how is that done? There's a Window Object returned from create, but it has no useful hooks in it - just a few attributes. Nothing in the documentation suggests how to do that, and I don't see and DOM tree fo it anywhere.

Any ideas?

Thanks a lot!


回答1:


I discovered the answer, after more reading and experimenting: When creating the window, you need to create a tab and attach it to the window. So, your create createData object will contain a URL attribute which simply points to the HTML page (with CSS, JS, whatever you want) sitting in (by default) the root folder of the extension (so if you want your HTML files kept elsewhere, simply prefix the filename with a path). Now, when the window is created, it's created with one tab (which, from the UI perspective doesn't even show as a tab - which is nice) and that tab contains the content you want.




回答2:


Try this I have not tested it myself but might give you some idea....

(function(){ chrome.windows.create({ type: 'panel', url: "https://www.google.co.in/" }, function (newWindow) { console.log(newWindow); chrome.tabs.executeScript(newWindow.tabs[0].id, { code: 'document.write("hello world");' }); }); })()

and also as mention in this Chromium Projects link about panels you can manipulate pane same as other windows types.



来源:https://stackoverflow.com/questions/16723337/how-to-insert-html-js-into-window-of-type-panel-created-by-chrome-windows-cr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!