问题
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