11browser . runtime . sendMessage ( { method : "getStyles" , url : getIdUrl ( ) || location . href } ) . then ( ( response ) => {
22 if ( response . length == 0 ) {
3- sendEvent ( "styleCanBeInstalled " ) ;
3+ sendEvent ( "styleCanBeInstalledChrome " ) ;
44 return ;
55 }
66 let installedStyle = response [ 0 ] ;
77 if ( Object . keys ( installedStyle . advanced . saved ) . length > 0 ) {
8- sendEvent ( "styleCanBeUpdated " ) ;
8+ sendEvent ( "styleCanBeUpdatedChrome " ) ;
99 return ;
1010 }
1111 // maybe an update is needed
@@ -14,15 +14,15 @@ browser.runtime.sendMessage({method: "getStyles", url: getIdUrl() || location.hr
1414 if ( md5_url && installedStyle . md5Url && installedStyle . originalMd5 ) {
1515 getURL ( md5_url ) . then ( ( md5 ) => {
1616 if ( md5 == installedStyle . originalMd5 ) {
17- sendEvent ( "styleAlreadyInstalled " ) ;
17+ sendEvent ( "styleAlreadyInstalledChrome " ) ;
1818 return ;
1919 } else {
20- sendEvent ( "styleCanBeUpdated " ) ;
20+ sendEvent ( "styleCanBeUpdatedChrome " ) ;
2121 return ;
2222 }
2323 } ) ;
2424 } else {
25- sendEvent ( "styleCanBeInstalled " ) ;
25+ sendEvent ( "styleCanBeInstalledChrome " ) ;
2626 return ;
2727 }
2828} ) ;
@@ -150,37 +150,58 @@ function getAdvanced() {
150150 } ) ;
151151}
152152document . addEventListener ( "stylishInstall" , usoInstall , false ) ;
153+ document . addEventListener ( "stylishInstallChrome" , usoInstall , false ) ;
153154document . addEventListener ( "stylishUpdate" , usoInstall , false ) ;
154155
156+ window . postMessage ( {
157+ direction : 'from-content-script' ,
158+ message : 'StylishInstalled' ,
159+ } , '*' ) ;
155160// Fix a uso bug
156161if ( IS_CHROME ) {
157- let src = document . createElement ( 'script' ) ;
158- src . innerHTML = '(function() {\
159- let fixObserver = new MutationObserver(function(mutations) {\
160- checkInstallButton();\
161- });\
162- function checkInstallButton() {\
163- let buttons = ["update_style_button"];\
164- let inited = 0;\
165- for (let btnId of buttons) {\
166- if (document.getElementById(btnId)) {\
167- inited++;\
168- if (document.getElementById(btnId).getAttribute("data-xstyle")) {\
169- continue;\
170- }\
171- document.getElementById(btnId).setAttribute("data-xstyle", 1);\
172- document.getElementById(btnId).addEventListener("click", function() {\
173- let newEvent = new CustomEvent("stylishInstall", {detail: null});\
174- document.dispatchEvent(newEvent);\
175- });\
176- }\
177- }\
178- if (inited === buttons.length) {\
179- fixObserver.disconnect();\
180- fixObserver = null;\
181- }\
182- }\
183- fixObserver.observe(document.body, {childList: true, subtree: true});\
184- })();' ;
185- document . body . appendChild ( src ) ;
162+ const src = document . createElement ( 'script' ) ;
163+ src . innerHTML = `(function() {
164+ const originImage = window.Image;
165+ window.Image = function(...args) {
166+ const newImage = new originImage(...args);
167+ Object.defineProperty(newImage, 'src', {
168+ set: function(newValue) {
169+ if (newValue.includes('chrome-extension://')) {
170+ setTimeout(() => {
171+ newImage.onload();
172+ }, 0);
173+ } else {
174+ newImage.src = newValue;
175+ }
176+ }
177+ });
178+ return newImage;
179+ };
180+ let fixObserver = new MutationObserver(function(mutations) {
181+ checkInstallButton();
182+ });
183+ function checkInstallButton() {
184+ let buttons = ["update_style_button"];
185+ let inited = 0;
186+ for (let btnId of buttons) {
187+ if (document.getElementById(btnId)) {
188+ inited++;
189+ if (document.getElementById(btnId).getAttribute("data-xstyle")) {
190+ continue;
191+ }
192+ document.getElementById(btnId).setAttribute("data-xstyle", 1);
193+ document.getElementById(btnId).addEventListener("click", function() {
194+ let newEvent = new CustomEvent("stylishInstall", {detail: null});
195+ document.dispatchEvent(newEvent);
196+ });
197+ }
198+ }
199+ if (inited === buttons.length) {
200+ fixObserver.disconnect();
201+ fixObserver = null;
202+ }
203+ }
204+ fixObserver.observe(document.body, {childList: true, subtree: true});
205+ })();` ;
206+ document . documentElement . appendChild ( src ) ;
186207}
0 commit comments