Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Fix #362, follow redirect page in I'm Lucky searches
Browse files Browse the repository at this point in the history
Sometimes Google has started to interrupt I'm Feeling Luck search URLs with a redirect page. This follows those redirect.
  • Loading branch information
ianb committed Oct 7, 2019
1 parent e67ea25 commit 40d304c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 29 additions & 1 deletion extension/background/intentRunner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals log, intentParser, telemetry, catcher */
/* globals log, intentParser, telemetry, catcher, searching */
// This gets used elsewhere as a namespace for the intent modules:
this.intents = {};

Expand Down Expand Up @@ -80,6 +80,34 @@ this.intentRunner = (function() {
return browser.tabs.create(options);
}

async createTabGoogleLucky(query) {
const searchUrl = searching.googleSearchUrl(query, true);
const tab = await this.createTab({ url: searchUrl });
return new Promise((resolve, reject) => {
function onUpdated(tabId, changeInfo, tab) {
const url = tab.url;
if (url.startsWith("about:blank")) {
return;
}
console.log("!!! tab update", tab.status, tab.url);
const isGoogle = /^https:\/\/www.google.com\//.test(tab.url);
const isRedirect = /^https:\/\/www.google.com\/url\?/.test(url);
if (!isGoogle || isRedirect) {
if (isRedirect) {
// This is a URL redirect:
const params = new URL(url).searchParams;
const newUrl = params.get("q");
browser.tabs.update(tab.id, { url: newUrl });
}
// We no longer need to listen for updates:
browser.tabs.onUpdated.removeListener(onUpdated, { tabId: tab.id });
resolve(tab);
}
}
browser.tabs.onUpdated.addListener(onUpdated, { tabId: tab.id });
});
}

onError(message) {
// Can be overridden
}
Expand Down
3 changes: 1 addition & 2 deletions extension/intents/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ this.intents.navigation = (function() {
(bring me | go | navigate | show me) (to | open | find |) [query]
`,
async run(context) {
const url = searching.googleSearchUrl(context.slots.query, true);
await context.createTab({ url });
await context.createTabGoogleLucky(context.slots.query);
browser.runtime.sendMessage({
type: "closePopup",
sender: "navigate",
Expand Down
9 changes: 4 additions & 5 deletions extension/services/youtube/youtube.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals intents, serviceList, searching, content, util */
/* globals intents, serviceList, content, util */

this.services.youtube = (function() {
const SUPPORTED_URLS = /^https:\/\/www.youtube.com\/watch/i;
Expand All @@ -24,10 +24,9 @@ this.services.youtube = (function() {

class YouTube extends serviceList.Service {
async playQuery(query) {
this.tab = await this.context.createTab({
url: searching.googleSearchUrl(`${query} youtube.com`, true),
active: true,
});
this.tab = await this.context.createTabGoogleLucky(
`${query} youtube.com`
);
this.tabCreated = true;
// We only test for audibility if the URL seems correct
const loadedTab = await waitForUrl(this.tab.id, {
Expand Down

0 comments on commit 40d304c

Please sign in to comment.