Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

javascript - 在Chrome扩展程序的生成窗口中运行Javascript(Run Javascript on Spawned Window in Chrome Extension)

I am building a Chrome extension, and one of the behaviors is to spawn a popup window with a specified size and location:(我正在构建一个Chrome扩展程序,其行为之一是生成具有指定大小和位置的弹出窗口:)

url = "https://google.com" chrome.windows.create( { 'url': url, 'type': 'popup', 'width': Math.floor(screen.availWidth / 2), 'height': Math.floor(screen.availHeight / 2), 'left': Math.floor(screen.availWidth / 2), 'top': 0 }, function(window) {} ); At this point I need to run some Javascript on the spawned page.(此时,我需要在生成的页面上运行一些Javascript。) I understand that I can declare a content script in my manifest that matches url , however the url is actually dynamic, and can be pretty much anything.(我知道我可以在清单中声明与url匹配的内容脚本,但是url实际上是动态的,几乎可以是任何东西。) So, my best guess right now would be to add a content script with matcher https://*/* , and through some other means determine that this is the page which was spawned by the extension - either through adding some query param, or through the extension messaging system.(因此,我现在最好的猜测是添加一个带有匹配器https://*/*的内容脚本,并通过其他方式确定这是扩展程序生成的页面-通过添加一些查询参数,或者通过扩展消息系统。) This approach seems kind of hacky, though.(但是,这种方法似乎有点怪异。) Does anyone know of a more 'proper' way to do this?(有谁知道一种更“合适”的方式来做到这一点?)   ask by max pleaner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use chrome.tabs.executeScript inside the callback:(在回调内使用chrome.tabs.executeScript :)

chrome.windows.create({ /*params*/ }, w => { chrome.tabs.executeScript(w.tabs[0].id, {file: 'content.js'}); }); Notes:(笔记:) The site or "<all_urls>" must be added in "permissions" in manifest.json.(该站点或"<all_urls>"必须添加在manifest.json的"permissions"中。) The returned window is not the JavaScript window so it's best to use a different name.(返回的窗口不是JavaScript window因此最好使用其他名称。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...