Skip to content

Commit

Permalink
3.4.0 some fix and improve
Browse files Browse the repository at this point in the history
Make sure no await before asking for browser permission
Improve and fix some ajax
Add some dictionaries
  • Loading branch information
garywill committed Dec 30, 2023
1 parent 7fe3f6c commit d8d95b1
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 97 deletions.
37 changes: 36 additions & 1 deletion src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function ajax_execute(ajax, keyword)
{
console.debug("ajax_execute()");
const before_start = 100;
var slow = 10;
var slow = 20;
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -90,11 +90,27 @@ async function ajax_execute(ajax, keyword)

var web_inputbox;

await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.click();

await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.focus();


await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keypress", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keydown", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 1);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keyup", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 2);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new Event( "input", { bubbles:true } ) );
Expand All @@ -103,10 +119,27 @@ async function ajax_execute(ajax, keyword)
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new Event( "change", { bubbles:true } ) );




await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.value = keyword ;



await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keypress", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 5);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keydown", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 1);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new KeyboardEvent( "keyup", { key: " ", keyCode: 32, bubbles: true } ) );

await sleep(slow * 2);
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new Event( "input", { bubbles:true } ) );
Expand All @@ -115,6 +148,8 @@ async function ajax_execute(ajax, keyword)
web_inputbox = document.querySelectorAll(queryStr)[0];
web_inputbox.dispatchEvent ( new Event( "change", { bubbles:true } ) );



if (pressEnter)
{
await sleep(slow * 5);
Expand Down
19 changes: 17 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ async function set_contextmenu() {
break;
case "popup":
default:
onMenuClickFunc_open = function() { chrome.browserAction.openPopup() ; } ;
onMenuClickFunc_open = function() {
// NOTE !!!! NOTICE !! ` openPopup ` not allow above have ` await `
if (mv>=3)
chrome.action.openPopup() ;
else
chrome.browserAction.openPopup() ;
} ;
break;
}

Expand Down Expand Up @@ -203,6 +209,7 @@ async function setContextMenuOrKeyShortcutCopyBehavior()
var setting_copyOnContextMenuOrKey = await get_addon_setting_local('copyOnContextMenuOrKey') ;
if (setting_copyOnContextMenuOrKey)
onMenuClickOrKeyFunc_copy = function(str) {
// NOTE !!!! NOTICE !! ` permissions.request ` not allow above have ` await `
chrome.permissions.request( { permissions: ['clipboardWrite'] } , r=>console.log(r) );
navigator.clipboard.writeText(str) ;
} ;
Expand All @@ -214,18 +221,26 @@ async function setContextMenuOrKeyShortcutCopyBehavior()

chrome.commands.onCommand.addListener(async function (command, tab) { // 'tab' is only in mv3
if (command == "selection_as_search_then_open_popup")
try { chrome.browserAction.openPopup() ; } catch(err) { console.warn(err) }
try {
// NOTE !!!! NOTICE !! ` openPopup ` not allow above have ` await `
if (mv>=3)
chrome.action.openPopup() ;
else
chrome.browserAction.openPopup() ;
} catch(err) { console.warn(err) }
else if (command == "selection_as_search_then_open_sidebar")
try { chrome.sidebarAction.open() ; } catch(err) { console.warn(err) }

if (command.startsWith("selection_as_search")) {

// console.debug("onCommand selection_as_search");

// NOTE !!!! NOTICE !! ` permissions.request ` not allow above have ` await `
await chrome.permissions.request({ permissions: ["activeTab"] });

if (mv >= 3)
{
// NOTE !!!! NOTICE !! ` permissions.request ` not allow above have ` await `
await chrome.permissions.request({ permissions: ["scripting"] });

chrome.scripting.executeScript( {
Expand Down
115 changes: 59 additions & 56 deletions src/engine_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,54 +359,55 @@ function getDataForGo(engine,btn,dbname=null)
}


var newTabBringFront;
async function goEngBtn(engine,btn,keyword,dbname=null)
{
var newTabIndex = -1 ;
if (window.run_env != "http_web") {
var setting_newTabPos = await get_addon_setting_local('newTabPos');
switch (setting_newTabPos){
case 'left-all': newTabIndex = 0 ; break;
case 'right-all': newTabIndex = 99999999 ; break;
case 'left-current':
case 'right-current':
await chrome.tabs.query(
{ currentWindow: true , active: true},
async function(r) {
console.log(r);
newTabIndex = r[0].index ;
if (setting_newTabPos == 'right-current')
newTabIndex = r[0].index+1;
}
) ;
break;

default: newTabIndex = 0;
}

newTabBringFront = false;

//-----------------------------
async function zzsleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for ( var zz=0; zz<1000; zz++) {
if (newTabIndex != -1)
break;
else if (zz==999)
{
console.error('Waiting for newTabIndex, but timeout !')
return;
async function calcNewTabIndex() {
var newTabIndex = -1;
var setting_newTabPos = await get_addon_setting_local('newTabPos');
switch (setting_newTabPos){
case 'left-all': newTabIndex = 0 ; break;
case 'right-all': newTabIndex = 99999999 ; break;
case 'left-current':
case 'right-current':
await chrome.tabs.query(
{ currentWindow: true , active: true},
async function(r) {
console.log(r);
newTabIndex = r[0].index ;
if (setting_newTabPos == 'right-current')
newTabIndex = r[0].index+1;
}
await zzsleep(5);
}
//------------------------------
) ;
break;

default: newTabIndex = 0;
}


//-----------------------------
async function zzsleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for ( var zz=0; zz<1000; zz++) {
if (newTabIndex != -1)
break;
else if (zz==999)
{
console.error('Waiting for newTabIndex, but timeout !');
break;
}
await zzsleep(5);
}
//------------------------------
return newTabIndex;
}

var newTabBringFront = false;
var newTabIndex = -1 ;
async function goEngBtn(engine,btn,keyword,dbname=null)
{


if ( dbname == "browser" ) {

newTabIndex = await calcNewTabIndex();
if (isFirefox)
{
const newTab = ( await browser.tabs.create({url:"about:blank", active: newTabBringFront, index: newTabIndex}) );
Expand Down Expand Up @@ -481,6 +482,7 @@ async function goEngBtn(engine,btn,keyword,dbname=null)
go_full_url(keyword, data.full_url, data.charset,use_referer);
else
{
newTabIndex = await calcNewTabIndex();
await open_connecting_page(dbname, engine, btn, keyword, newTabIndex);
}
return;
Expand All @@ -495,6 +497,16 @@ async function goEngBtn(engine,btn,keyword,dbname=null)
if (window.run_env == "http_web") {
}


if (mv >= 3) {
// NOTE !!!! NOTICE !! ` permissions.request ` not allow above have ` await `
var r = await chrome.permissions.request({ permissions: ["scripting"] });
if ( !r ) {
console.error("Failed to get 'scripting' permission");
return;
}
}

var permis_have;
if (isFirefox) {
permis_have = await browser.permissions.getAll();
Expand All @@ -504,18 +516,6 @@ async function goEngBtn(engine,btn,keyword,dbname=null)
}) );
}

if (mv >= 3)
{
if ( ! permis_have['permissions'].includes('scripting') )
{
var r = await chrome.permissions.request({ permissions: ["scripting"] });
if ( !r ) {
console.error("Failed to get 'scripting' permission");
return;
}

}
}


var host_permis_needed = removeUrlParts(data.action) + '*';
Expand All @@ -529,7 +529,9 @@ async function goEngBtn(engine,btn,keyword,dbname=null)
return;
}




newTabIndex = await calcNewTabIndex();

var newTab;
if (isFirefox) {
Expand Down Expand Up @@ -586,7 +588,8 @@ async function goEngBtn(engine,btn,keyword,dbname=null)
if (window.run_env == "http_web")
form_submit(fparams, data.action, data.charset, data.method, use_referer);
else{
await open_connecting_page(dbname, engine, btn, keyword, newTabIndex);
newTabIndex = await calcNewTabIndex();
await open_connecting_page(dbname, engine, btn, keyword, newTabIndex);
}
}

Expand Down
Loading

0 comments on commit d8d95b1

Please sign in to comment.