Google Chrome extension - to open new tab directed to url when a certain url is searched in current tab?

邮差的信 提交于 2019-12-13 18:13:04

问题


I am very new (started today) to writing chrome extensions but need to write a 'fairly simple' one for tomorrow...

I am really struggling to get my head round it though!

I need to write an extension that opens a new tab or window (whichever is easier!) directed at a predefined url is opened when a certain URL is searched in the current.

An example of this would be if the user typed in www.facebook.com I am trying to get the extension to then open a new tab/window with www.google.com in.

Any help would be much appreciated! Thanks Will


回答1:


In your html of the extension you can specify an anchor tag for the action button which opens a new tab like this

<a href="some url" target="_newtab">content of the anchor</a>

or if you want to do it programmatically you can

window.open('http://google.com','_newtab');



回答2:


You cant get at the contents of the omnibox unless you use the omnibox api, which means they have to specify your keyword first before your extension can get at the contents. You could make it open another page once they have gone to a url. You could use a content script that has a matches field that matches the page your interested in and then open a page accordingly.

Content Scripts
http://code.google.com/chrome/extensions/content_scripts.html

Tabs
You can use the tab api to get the current tabs details if your not in a content script. If you are in a content script you can use window.location as stated by Encore PTL. You can also use the tab api to open a tab.
http://code.google.com/chrome/extensions/tabs.html



来源:https://stackoverflow.com/questions/9863106/google-chrome-extension-to-open-new-tab-directed-to-url-when-a-certain-url-is

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