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

Commit

Permalink
Fix #467, poll the search results for some changes
Browse files Browse the repository at this point in the history
This looks for a `<canvas>` and polls if it is present
  • Loading branch information
ianb committed Oct 23, 2019
1 parent 8c9eefb commit 00939e3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion extension/intents/search/queryScript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* globals communicate */

this.queryScript = (function() {
const CARD_SELECTOR = ".vk_c.card-section, .lr_container.mod";
const CARD_SELECTOR =
".vk_c.card-section, .lr_container.mod, div[role=widget], .act-card";

communicate.register("searchResultInfo", message => {
const hasSidebarCard = !!document.querySelector(".kp-header");
Expand All @@ -24,6 +25,8 @@ this.queryScript = (function() {

communicate.register("cardImage", message => {
const card = document.querySelector(CARD_SELECTOR);
// When it has a canvas it may dynamically update:
const hasWidget = !!card.querySelector("canvas");
const rect = card.getBoundingClientRect();
const canvas = document.createElementNS(
"http://www.w3.org/1999/xhtml",
Expand All @@ -38,6 +41,7 @@ this.queryScript = (function() {
width: rect.width,
height: rect.height,
src: canvas.toDataURL(),
hasWidget,
};
});
})();
40 changes: 40 additions & 0 deletions extension/intents/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ this.intents.search = (function() {
const exports = {};
// Close the search tab after this amount of time:
const CLOSE_TIME = 1000 * 60 * 60; // 1 hour
const CARD_POLL_INTERVAL = 200;
let closeTabTimeout = null;
let lastImage = null;
let _searchTabId;
const START_URL = "https://www.google.com";
let lastSearchInfo;
Expand Down Expand Up @@ -66,6 +68,38 @@ this.intents.search = (function() {
return browser.tabs.sendMessage(_searchTabId, message);
}

let cardPollTimeout;

function stopCardPoll() {
if (cardPollTimeout) {
clearTimeout(cardPollTimeout);
cardPollTimeout = null;
lastImage = null;
}
}

function pollForCard() {
stopCardPoll();
cardPollTimeout = setInterval(async () => {
const card = await callScript({ type: "cardImage" });
if (card.src === lastImage) {
return;
}
lastImage = card.src;
const response = await browser.runtime.sendMessage({
type: "showSearchResults",
card,
searchResults: lastSearchInfo.searchResults,
searchUrl: lastSearchInfo.searchUrl,
index: -1,
});
if (!response) {
// There's no listener
stopCardPoll();
}
}, CARD_POLL_INTERVAL);
}

exports.focusSearchResults = async message => {
const { searchUrl } = message;
const searchTabId = await openSearchTab();
Expand All @@ -88,6 +122,7 @@ this.intents.search = (function() {
(do a |) (search | query | find | find me | google | look up | lookup | look on | look for) (google | the web | the internet |) (for |) [query] (on the web |)
`,
async run(context) {
stopCardPoll();
await performSearch(context.slots.query);
const searchInfo = await callScript({ type: "searchResultInfo" });
lastSearchInfo = searchInfo;
Expand All @@ -103,6 +138,9 @@ this.intents.search = (function() {
searchUrl: searchInfo.searchUrl,
index: -1,
});
if (card.hasWidget) {
pollForCard();
}
} else {
context.keepPopup();
lastSearchIndex = 0;
Expand All @@ -128,6 +166,7 @@ this.intents.search = (function() {
(search |) next (search |) (result | item | page | article |)
`,
async run(context) {
stopCardPoll();
if (!lastSearchInfo) {
const e = new Error("No search made");
e.displayMessage = "You haven't made a search";
Expand Down Expand Up @@ -164,6 +203,7 @@ this.intents.search = (function() {
(open | show | focus) results
`,
async run(context) {
stopCardPoll();
const tabId = await openSearchTab();
await context.makeTabActive(tabId);
},
Expand Down
2 changes: 2 additions & 0 deletions extension/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ this.popup = (function() {
} else if (message.type === "showSearchResults") {
lastSearchUrl = message.searchUrl;
ui.showSearchResults(message);
return Promise.resolve(true);
}
return undefined;
}

async function updateExamples() {
Expand Down

0 comments on commit 00939e3

Please sign in to comment.