Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

[OT] migrating from scripts to service_worker in js perl generated extensions for chrome

by Discipulus (Canon)
on Feb 08, 2023 at 10:44 UTC ( [id://11150238]=perlquestion: print w/replies, xml ) Need Help??

Discipulus has asked for the wisdom of the Perl Monks concerning the following question:

Pardon me fellow monks and nuns for this heretic question..

Nine years ago (!!) I offered you my Automatic chrome extension generator and today I stumbled on errors in the extensions tab of chrome:

Manifest version 2 is deprecated, and support will be removed in 2023. + See https://developer.chrome.com/blog/mv2-transition/ for more detai +ls.

So I tried to simply put "manifest_version": 3, but then it complained about script no more supported and to switch to service_worker so I changed this.. and then onclick to chrome.contextMenus.onClicked.addListener .. and then..

Can some kind soul look the below JS code and help me to fix it?

The goal is simple: create a custom context menu entry acting on selected text to craft a GET to open in a new tab.

#!/usr/bin/perl use strict; use warnings; # supporting manifest v 3 # https://developer.chrome.com/blog/mv2-transition/ # https://developer.chrome.com/docs/extensions/mv3/migrating_to_servic +e_workers/ # January 2023: The Chrome browser will no longer run Manifest V2 exte +nsions. # Developers may no longer push updates to existing Manifest V2 extens +ions. my $version = 2; my $folder = $ARGV[0]; my $url = $ARGV[1]; die "$0 Directory_Name URL" unless $ARGV[1]; (my $descr = $folder) =~ s/_+/ /g; my $longname = 'Perl genarated extension - '.$descr; mkdir $folder or die "Cannot create $folder: $!"; chdir $folder or die "Cannot enter $folder: $!"; # the manifest open MANIF, '>', 'manifest.json' or die "Cannot open a file to write i +n: $!"; my $manifest = '{ "manifest_version": 3, "description": "'.$descr.'", "background": { "service_worker": "background.js"}, "name": "'.$longname.'", "permissions": [ "contextMenus", "tabs" ], "version": "1.0" }'; print MANIF $manifest; close MANIF; # the jscript open JSCRIPT, '>', 'background.js' or die "Cannot open a file to write + in: $!"; my $background ='function customfunc(info) { var searchstring = info.selectionText; chrome.tabs.create({url: "'.$url.'" + searchstring}) } chrome.contextMenus.onClicked.addListener(function(info, tab) { if (info.menuItemId == "'.$descr.'") });'; print JSCRIPT $background; close JSCRIPT;

In the case it is useful here the relevant part of diff -ing the two versions

16c26 < "manifest_version": 2, --- > "manifest_version": 3, 19c29 < "scripts": ["background.js"]}, --- > "service_worker": "background.js"}, 33c43,47 < chrome.contextMenus.create({title: "'.$descr.'", contexts:["selectio +n"], onclick: customfunc});'; --- > > chrome.contextMenus.onClicked.addListener(function(info, tab) { > if (info.menuItemId == "'.$descr.'") > });'; > 35c49 < close JSCRIPT; \ No newline at end of file --- > close JSCRIPT;

L*

update maybe something like this?

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on [OT] migrating from scripts to service_worker in js perl generated extensions for chrome
  • Select or Download Code

Replies are listed 'Best First'.
Re: [OT] migrating from scripts to service_worker in js perl generated extensions for chrome -- solved
by Discipulus (Canon) on Feb 08, 2023 at 20:19 UTC
    After some try and error and error and error (damned error reporting!) this version works!

    #!/usr/bin/perl use strict; use warnings; my $folder = $ARGV[0]; my $url = $ARGV[1]; (my $descr = $folder) =~ s/_+/ /g; my $longname = 'Perl genarated extension - '.$descr; mkdir $folder or die "Cannot create $folder: $!"; chdir $folder or die "Cannot enter $folder: $!"; # the manifest open MANIF, '>', 'manifest.json' or die "Cannot open a file to write i +n: $!"; my $manifest = '{ "manifest_version": 3, "description": "'.$descr.'", "background": { "service_worker": "background.js"}, "name": "'.$longname.'", "permissions": [ "contextMenus", "tabs" ], "version": "1.0" }'; print MANIF $manifest; close MANIF; # the jscript open JSCRIPT, '>', 'background.js' or die "Cannot open a file to write + in: $!"; my $background = 'chrome.runtime.onInstalled.addListener(function() { chrome.contextMenus.create({ title: "'.$descr.'", contexts: ["selection"], id: "'.$descr.'" }); }); chrome.contextMenus.onClicked.addListener(function (info, tab) { if (info.menuItemId === "'.$descr.'") { let getToSite = "'.$url.'" + info.selectionText chrome.tabs.create({index: tab.index + 1, url: getToSite, sele +cted: true}); } })'; print JSCRIPT $background; close JSCRIPT;

    here the diff output from the original 9 years old code:

    16c16 < "manifest_version": 2, --- > "manifest_version": 3, 19c19 < "scripts": ["background.js"]}, --- > "service_worker": "background.js"}, 28,33c28,42 < my $background ='function customfunc(info) < { < var searchstring = info.selectionText; < chrome.tabs.create({url: "'.$url.'" + searchstring}) < } < chrome.contextMenus.create({title: "'.$descr.'", contexts:["selectio +n"], onclick: customfunc});'; --- > my $background = 'chrome.runtime.onInstalled.addListener(function() +{ > chrome.contextMenus.create({ > title: "'.$descr.'", > contexts: ["selection"], > id: "'.$descr.'" > }); > }); > > chrome.contextMenus.onClicked.addListener(function (info, tab) { > if (info.menuItemId === "'.$descr.'") { > let getToSite = "'.$url.'" + info.selectionText > chrome.tabs.create({index: tab.index + 1, url: getToSite, se +lected: true}); > } > })'; >

    see you in 2032 :)

    L*

    UPDATE If you want to add the website icon to the context menu you can download it (generally available under the root of the site like in: hhtps://example.com/favicon.ico ), transform it to a .png file (not rename; transform) and add it to the manifest.json file, after the version like in:

    { "manifest_version": 3, "description": "Description", "background": { "service_worker": "background.js"}, "name": "Perl genarated extension - Description", "permissions": [ "contextMenus", "tabs" ], "version": "1.0", "icons": { "128": "favicon.png", "16": "favicon.png", "32": "favicon.png", "48": "favicon.png", "64": "favicon.png" } }

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11150238]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found