81444

Chrome extensions: open link in new tab upon loading of a particular page?

Question:

I'd like to create a Chrome extension where, when a particular URL opens, another URL opens automatically in a new tab. Note that I'd like the new tab to open as soon as the first URL begins to load, not once it's finished loading.

So basically, you'd click on a link to the first URL, and two tabs would open; one containing the first URL, one containing the second URL.

However, I really don't know where to begin. I don't think it can be a content script because they can't access chrome.tabs. So I'm trying to use a background page but it's not working. Here's my bg.html:

<html> <body> <script type="text/javascript"> var o = new Object; chrome.tabs.onCreated.addListener( function(Tab tab) { chrome.tabs.create(o); } ); </script> </body> </html>

and my manifest:

{ "name": "My First Extension", "version": "1.0", "description": "The first extension that I made.", "background_page": "bg.html", "permissions": [ "tabs" ] }

Answer1:

In a background page:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if(changeInfo.status == "loading" && tab.url == "http://old/url") { chrome.tabs.create({url: "http://new/url"}); } });

Recommend

  • Updating Ui Components in Activity based on MediaPlayer object from Service While Playing music file
  • Image change opacity on hover without jQuery
  • Initialize specific subscript item in array by c++ [duplicate]
  • Automatically change the position of objects when using different devices
  • adjust iframe height based on the content
  • css background images not always displayed
  • CloseOptionsMenu doesn't work?
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Most efficient way to move table rows from one table to another
  • AppleScript : find open tab in safari by name and open it
  • Webgrid not refreshing after delete MVC
  • Jquery UI tool tip close icon
  • How to delay loading a property with linq to sql external mapping?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Importing jscolor library in angular 2
  • Weird JavaScript statement, what does it mean?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Traverse Array and Display in markup
  • Data Validation Drop Down Box Arrow Disappearing
  • Matrix multiplication with MKL
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • Converting MP3 duration time