Skip to content

Commit

Permalink
fix current tab scripting with new API
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Jun 6, 2024
1 parent 12de4ed commit 0d49412
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 81 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wishspace-webextension-vue",
"version": "4.0.2",
"version": "4.0.4",
"private": true,
"scripts": {
"dev": "vue-cli-service serve",
Expand All @@ -25,16 +25,19 @@
"vuex": "^3.6.2"
},
"devDependencies": {
"@babel/core": "^7.12.16",
"@babel/eslint-parser": "^7.12.16",
"@vue/cli-plugin-babel": "~5.0.8",
"@vue/cli-plugin-eslint": "~5.0.8",
"@vue/cli-service": "~5.0.8",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"node-sass": "^6.0.0",
"sass-loader": "^10.0.0",
"vue-template-compiler": "^2.7.15",
"@babel/eslint-parser": "^7.12.16",
"@babel/core": "^7.12.16"
"vue-template-compiler": "^2.7.15"
},
"resolutions": {
"node-gyp": "^10"
},
"eslintConfig": {
"root": true,
Expand Down
36 changes: 18 additions & 18 deletions public/js/tab.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var image = document.querySelector('meta[property=\'og:image\']');
var url = document.querySelector('meta[property=\'og:url\']');
var title = document.querySelector('meta[property=\'og:title\']');
var description = document.querySelector('meta[property=\'og:description\']');
var image = document.querySelector("meta[property='og:image']");
var url = document.querySelector("meta[property='og:url']");
var title = document.querySelector("meta[property='og:title']");
var description = document.querySelector("meta[property='og:description']");
if (image) {
image = image.getAttribute('content');
image = image.getAttribute('content');
} else {
// cas spécifique pour amazon
image = document.querySelector('img[data-old-hires]');
if (image) {
image = image.getAttribute('data-old-hires');
}
// cas spécifique pour amazon
image = document.querySelector('img[data-old-hires]');
if (image) {
image = image.getAttribute('data-old-hires');
}
}
if (url) {
url = url.getAttribute('content');
url = url.getAttribute('content');
}
if (title) {
title = title.getAttribute('content');
title = title.getAttribute('content');
}
if (description) {
description = description.getAttribute('content');
description = description.getAttribute('content');
}

({
image: image || '',
url: url || '',
title: title || '',
description: description || ''
});
image: image || '',
url: url || '',
title: title || '',
description: description || '',
});
4 changes: 2 additions & 2 deletions public/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Oh My Wish Extension",
"short_name": "Oh My Wish",
"description": "Cette extension permet de rajouter facilement des souhaits sur le site de Oh My Wish",
"version": "4.0.3",
"version": "4.0.4",
"author": "Anselme Chorein",
"homepage_url": "https://ohmywish.me",
"options_page": "options.html",
Expand All @@ -23,6 +23,6 @@
"default_popup": "popup.html",
"default_title": "Ajouter un souhait à votre liste Oh My Wish"
},
"permissions": ["activeTab", "storage"],
"permissions": ["activeTab", "storage", "scripting"],
"host_permissions": ["https://api.ohmywish.me/*"]
}
4 changes: 2 additions & 2 deletions public/manifest.edge.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Oh My Wish Extension",
"short_name": "Oh My Wish",
"description": "Cette extension permet de rajouter facilement des souhaits sur le site de Oh My Wish",
"version": "4.0.3",
"version": "4.0.4",
"author": "Anselme Chorein",
"homepage_url": "https://ohmywish.me",
"options_page": "options.html",
Expand All @@ -23,6 +23,6 @@
"default_popup": "popup.html",
"default_title": "Ajouter un souhait à votre liste Oh My Wish"
},
"permissions": ["activeTab", "storage"],
"permissions": ["activeTab", "storage", "scripting"],
"host_permissions": ["https://api.ohmywish.me/*"]
}
2 changes: 1 addition & 1 deletion public/manifest.firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Oh My Wish Extension",
"short_name": "Oh My Wish",
"description": "Cette extension permet de rajouter facilement des souhaits sur le site de Oh My Wish",
"version": "4.0.3",
"version": "4.0.4",
"icons": {
"16": "img/icon16.png",
"48": "img/icon48.png",
Expand Down
9 changes: 4 additions & 5 deletions src/App-Popup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export default {
// retrieved users lists and follows from API (mixins)
this.getLists();
// Get current tab URL (mixins)
this.getTabs(tabs => {
const tab = tabs[0];
this.getCurrentTab(tab => {
// get data from persistent cache (mixins)
this.storageGet(
{
Expand All @@ -68,11 +67,11 @@ export default {
this.wishModel.happiness = this.getDataFromCache('happiness', '', storage); // mixins
// Parse DOM to get more informations (mixins)
this.executeScriptOnTab(results => {
if (!results) {
this.executeScriptOnTab(result => {
if (!result) {
console.log('Error while executing script on tab');
return; // error
}
const result = results[0];
const nom = this.getDataFromCache('nom', result.title, storage); // mixins
if (nom.length > 0) {
this.wishModel.nom = nom;
Expand Down
16 changes: 12 additions & 4 deletions src/mixins/ChromeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ export const chromeUtils = {
callback(params);
}
},
getTabs(callback) {
getCurrentTab(callback) {
if (!devMode) {
chrome.tabs.query({ active: true, currentWindow: true }, callback);
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (chrome.runtime.lastError) console.error(chrome.runtime.lastError);
// `tab` will either be a `tabs.Tab` instance or `undefined`.
callback(tab);
});
} else {
callback([{ url: 'http://localhost', title: 'Dev mode' }]);
}
},
executeScriptOnTab(callback) {
if (!devMode) {
chrome.tabs.executeScript({ file: '/js/tab.js' }, callback);
this.getCurrentTab(tab => {
chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['/js/tab.js'] }).then(injectionResults => {
callback(injectionResults[0].result);
});
});
} else {
callback([{ url: 'http://localhost:8080', title: 'Dev mode', description: 'description', image: 'http://localhost:8080/img/brand.png' }]);
callback({ url: 'http://localhost:8080', title: 'Dev mode', description: 'description', image: 'http://localhost:8080/img/brand.png' });
}
},
},
Expand Down
Loading

0 comments on commit 0d49412

Please sign in to comment.